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

Unified parsers.

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