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::
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
.. 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
"""
-import re, types, itertools, logging as log
+import re, types, itertools
import traceback
-
+from logs import log
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):