Commit 874b12bf authored by Brice Videau's avatar Brice Videau

Unified parsers.

parent 075cc6cf
......@@ -216,6 +216,12 @@ class Literal(Expression):
v = self.value
if isinstance(v, str):
return repr(v)
elif v is None:
return "none"
elif v is True:
return "true"
elif v is False:
return "false"
else:
return "{}".format(v)
......
......@@ -21,15 +21,17 @@ list: '[' list_item ']'
list_item: list_item ',' value
| value;
value: none
| boolean
| btrue
| bfalse
| string
| identifier
| integer
| float;
terminals
none: /None/ {prefer};
boolean: /True|False/ {prefer};
none: /none/ {prefer};
btrue: /true/ {prefer};
bfalse: /false/ {prefer};
identifier: /[a-zA-Z_][a-zA-Z_0-9]*/;
string: /"([^\0\t\n\r\f"\\]|\\[0tnrf"\\])+"|'([^\0\t\n\r\f'\\]|\\[0tnrf'\\])+'/;
integer: /-?[0-9]+/;
......@@ -53,8 +55,9 @@ _actions["list_item"] = [
lambda _, n: n[0] + [n[2]],
lambda _, n: [n[0]]
]
_actions["none"] = lambda _, value: Literal(value = eval(value))
_actions["boolean"] = lambda _, value: Literal(value = eval(value))
_actions["none"] = lambda _, value: Literal(value = None)
_actions["btrue"] = lambda _, value: Literal(value = True)
_actions["bfalse"] = lambda _, value: Literal(value = False)
_actions["identifier"] = lambda p, value: Variable(hyperparameter = p.extra.hyperparameter_by_name(value))
_actions["string"] = lambda _, value: Literal(value = eval(value))
_actions["float"] = lambda _, value: Literal(value = float(value))
......
......@@ -32,7 +32,7 @@ class TestExpression(unittest.TestCase):
e = ccs.Literal(value = 15)
self.assertEqual( "15" , str(e) )
e = ccs.Literal(value = None)
self.assertEqual( "None" , str(e) )
self.assertEqual( "none" , str(e) )
def test_variable(self):
h = ccs.NumericalHyperparameter()
......@@ -51,12 +51,12 @@ class TestExpression(unittest.TestCase):
def test_unary(self):
e = ccs.Expression.unary(t = ccs.NOT, node = True)
self.assertEqual( "!True", str(e) )
self.assertEqual( "!true", str(e) )
self.assertFalse( e.eval() )
def test_binary(self):
e = ccs.Expression.binary(t = ccs.OR, left = True, right = False)
self.assertEqual( "True || False", str(e) )
self.assertEqual( "true || false", str(e) )
self.assertTrue( e.eval() )
if __name__ == '__main__':
......
......@@ -39,23 +39,23 @@ class TestExpressionParser(unittest.TestCase):
self.assertFalse( res.eval() )
def test_boolean(self):
exp = "True"
exp = "true"
res = ccs.ccs_parser.parse(exp)
self.assertIsInstance( res, ccs.Literal )
self.assertEqual( True, res.eval() )
self.assertEqual( "True", res.__str__() )
exp = "False"
self.assertEqual( "true", res.__str__() )
exp = "false"
res = ccs.ccs_parser.parse(exp)
self.assertIsInstance( res, ccs.Literal )
self.assertEqual( False, res.eval() )
self.assertEqual( "False", res.__str__() )
self.assertEqual( "false", res.__str__() )
def test_none(self):
exp = "None"
exp = "none"
res = ccs.ccs_parser.parse(exp)
self.assertIsInstance( res, ccs.Literal )
self.assertIsNone( res.eval() )
self.assertEqual( "None", res.__str__() )
self.assertEqual( "none", res.__str__() )
if __name__ == '__main__':
......
......@@ -206,7 +206,9 @@ module CCS
def to_s
case value
when nil, String
when nil
"none"
when String
value.inspect
else
value.to_s
......
......@@ -43,10 +43,12 @@ module CCS
rule("]")
rule(",")
rule(:none => /nil/).as { |b|
rule(:none => /none/).as { |b|
Literal::new(value: nil) }
rule(:boolean => /true|false/).as { |b|
Literal::new(value: eval(b)) }
rule(:btrue => /true/).as { |b|
Literal::new(value: true) }
rule(:bfalse => /false/).as { |b|
Literal::new(value: false) }
rule(:float => /-?[0-9]+([eE][+-]?[0-9]+|\.[0-9]+([eE][+-]?[0-9]+)?)/).as {|num|
Literal::new(value: Float(num)) }
rule(:integer => /-?[0-9]+/).as { |num|
......@@ -58,7 +60,8 @@ module CCS
rule(:value) do |r|
r[:none]
r[:boolean]
r[:btrue]
r[:bfalse]
r[:string]
r[:identifier]
r[:float]
......
......@@ -63,11 +63,11 @@ class CConfigSpaceTestExpressionParser < Minitest::Test
def test_none
m = CCS::ExpressionParser.new.method(:parse)
exp = "nil"
exp = "none"
res = m[exp]
assert( res.kind_of? CCS::Literal )
assert_nil( res.eval )
assert_equal( "nil", res.to_s )
assert_equal( "none", res.to_s )
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment