X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=t%2Fmoe%2Fconfig_parser.py;h=2da6facfa2daa9062c78b2cfc9d9fe52cdddaf16;hb=3b2c0c783bf7284037f4c5c5ffd6582ee7b1f89c;hp=6dec8c9cd2a13d91e6a91b5ba32c5d7a8c933a7e;hpb=e9b511e32341f3712df0c285553378a140eaeb97;p=moe.git diff --git a/t/moe/config_parser.py b/t/moe/config_parser.py index 6dec8c9..2da6fac 100644 --- a/t/moe/config_parser.py +++ b/t/moe/config_parser.py @@ -1,10 +1,13 @@ 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:: @@ -22,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 @@ -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? -.. 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 """ @@ -44,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='', line=None, column=None):