X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=t%2Fmoe%2Fconfig_parser.py;h=ad10b02ed365d2d2cc47b2ca842039d169b3aa45;hb=976c301650130b3c6c7aa0df83e8c29ea2ffb650;hp=ce723baf8ff52900e6cc6aefa1ee1214fd2d6b33;hpb=750990d7dbbaf2740f290a26dab527ce4444cbfb;p=moe.git diff --git a/t/moe/config_parser.py b/t/moe/config_parser.py index ce723ba..ad10b02 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,17 +37,26 @@ 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 """ -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='', line=None, column=None): @@ -236,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)