]> mj.ucw.cz Git - moe.git/blobdiff - t/moe/config_parser.py
Fix in status parsing, add test, add update test
[moe.git] / t / moe / config_parser.py
index 217cd9177a5003cbea0a05d540eab0e2f2052e83..838d76b661edfc6b04120632d675f8ee96e5dcc6 100644 (file)
@@ -1,13 +1,13 @@
 r"""
-config_parser.py
-----------------
-
 Simple Moe configuration file syntax parser. 
 
-Generally, whitespace and comments are alowed everywhere except in variable names and inside expressions. 
-Also, COMMENT must not contain '\n'. 
+Generally, whitespace and comments are alowed everywhere except in variable names and inside expressions,
+``\\n`` ends a ``COMMENT``.
+
+``FILE``, ``BLOCK``, ``STATEMENT``, ``OPERATION``, ``SUBTREE``, ``CONDITION``, ``FORMULA``, ``AND``, ``OR`` 
+and ``NOT`` ignore any preceding whitespace. 
 
-FILE, BLOCK, STATEMENT, OPERATION, SUBTREE, CONDITION, FORMULA, AND, OR and NOT eat any preceding whitespace. 
+.. highlight:: none
 
 The configuration syntax is the following::
 
@@ -25,7 +25,8 @@ The configuration syntax is the following::
     SUBTREE = WS VARNAME WS '{' BLOCK WS '}'
     CONDITION = WS 'if' FORMULA WS '{' BLOCK WS '}'
 
-    FORMULA = WS (( EXPRESSION WS ( '!=' | '==' ) WS EXPRESSION ) | '(' AND WS ')' | '(' OR WS ')' | NOT )
+    FORMULA = WS (( EXPRESSION WS ( '!=' | '==' ) WS EXPRESSION ) | 
+      '(' AND WS ')' | '(' OR WS ')' | NOT )
     AND = FORMULA WS 'and' FORMULA
     OR = FORMULA WS 'or' FORMULA
     NOT = WS 'not' FORMULA 
@@ -37,7 +38,6 @@ The configuration syntax is the following::
 .. todo:: should whitespace (incl. '\n') be allowed (almost) everywhere? 
          can comment be anywhere whitespace can?
 .. note:: ';' or '\n' is currently required even after CONDITION and SUBTREE block 
-.. todo:: change to OPERATION only
 .. note:: Formula can contain additional/unnecessary parentheses
 """
 
@@ -47,6 +47,16 @@ import traceback
 import moe.config as cf
 
 
+def config_escape(s):
+  """
+  Escape any ``{``, ``}``, ``"`` and ``\\`` in the given string, making it safe for parsing.
+  """
+  s = s.replace('\\', '\\\\')
+  s = s.replace('{', '\\{')
+  s = s.replace('}', '\\}')
+  s = s.replace('"', '\\"')
+  return s
+
 class ConfigSyntaxError(cf.ConfigError):
 
   def __init__(self, msg, source='<unknown>', line=None, column=None):
@@ -239,8 +249,10 @@ class ConfigParser(object):
       op = 'SET'
     elif self.nexts(self.c_append):
       op = 'APPEND'
+    elif self.eof():
+      self.syntax_error('Unexpected end of file.')
     else:
-      self.syntax_error('Unknown operation.')
+      self.syntax_error('Unknown operation: %r...', self.peek(10))
     self.p_WS()
     exp = self.p_EXPRESSION()
     vname = (self.prefix+self.c_varname_sep+varname).lstrip(self.c_varname_sep)