]> mj.ucw.cz Git - moe.git/blobdiff - t/moe/config_parser.py
Minor updates (docs, status.py)
[moe.git] / t / moe / config_parser.py
index 6dec8c9cd2a13d91e6a91b5ba32c5d7a8c933a7e..2da6facfa2daa9062c78b2cfc9d9fe52cdddaf16 100644 (file)
@@ -1,10 +1,13 @@
 r"""
 Simple Moe configuration file syntax parser. 
 
 r"""
 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 eat any preceding whitespace. 
+``FILE``, ``BLOCK``, ``STATEMENT``, ``OPERATION``, ``SUBTREE``, ``CONDITION``, ``FORMULA``, ``AND``, ``OR`` 
+and ``NOT`` ignore any preceding whitespace. 
+
+.. highlight:: none
 
 The configuration syntax is the following::
 
 
 The configuration syntax is the following::
 
@@ -22,7 +25,8 @@ The configuration syntax is the following::
     SUBTREE = WS VARNAME WS '{' BLOCK WS '}'
     CONDITION = WS 'if' FORMULA WS '{' BLOCK WS '}'
 
     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 
     AND = FORMULA WS 'and' FORMULA
     OR = FORMULA WS 'or' FORMULA
     NOT = WS 'not' FORMULA 
@@ -33,8 +37,7 @@ The configuration syntax is the following::
 
 .. todo:: should whitespace (incl. '\n') be allowed (almost) everywhere? 
          can comment be anywhere whitespace can?
 
 .. 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:: ';' or '\\n' is currently required even after CONDITION and SUBTREE block 
 .. note:: Formula can contain additional/unnecessary parentheses
 """
 
 .. note:: Formula can contain additional/unnecessary parentheses
 """
 
@@ -44,6 +47,16 @@ import traceback
 import moe.config as cf
 
 
 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):
 class ConfigSyntaxError(cf.ConfigError):
 
   def __init__(self, msg, source='<unknown>', line=None, column=None):