]> mj.ucw.cz Git - eval.git/blobdiff - t/moe/config_parser.py
Writing status uses dump
[eval.git] / t / moe / config_parser.py
index 73b5406cbd6433e62712bde7b6b2f7f8717d3ae4..6dec8c9cd2a13d91e6a91b5ba32c5d7a8c933a7e 100644 (file)
@@ -1,47 +1,41 @@
-"""
-config_parser.py
-------------
-
+r"""
 Simple Moe configuration file syntax parser. 
 
 Simple Moe configuration file syntax parser. 
 
-TODO: decide neccessity of '()' in/around formulas
-TODO: check escaping in expressions
-TODO: should whitespace (incl. '\\n') be allowed (almost) everywhere?
-      can comment be anywhere whitespace can?
-
 Generally, whitespace and comments are alowed everywhere except in variable names and inside expressions. 
 Generally, whitespace and comments are alowed everywhere except in variable names and inside expressions. 
-Also, COMMENT must not contain '\\n'. 
+Also, COMMENT must not contain '\n'. 
 
 
-FILE, BLOCK, STATEMENT, OPERATION, SUBTREE, CONDITION, FORMULA, AND, OR and NOT eat any preceding whitespace. TODO: check?
+FILE, BLOCK, STATEMENT, OPERATION, SUBTREE, CONDITION, FORMULA, AND, OR and NOT eat any preceding whitespace. 
 
 
-The configuration syntax is the following:
+The configuration syntax is the following::
 
 
-FILE = BLOCK 
-BLOCK = WS | STATEMENT ( SEP STATEMENT )* 
+    FILE = BLOCK 
+    BLOCK = WS | STATEMENT ( SEP STATEMENT )* 
 
 
-SEP = ( '\\n' | ';' )
-WS = ( ' ' | '\\t' | '\\n' | COMMENT )*
+    SEP = ( '\n' | ';' )
+    WS = ( ' ' | '\t' | '\n' | COMMENT )*
 
 
-COMMENT = re('#[^\\n]*\\n')
+    COMMENT = re('#[^\n]*\n')
 
 
-STATEMENT = CONDITION | OPERATION | SUBTREE
+    STATEMENT = CONDITION | OPERATION | SUBTREE
 
 
-OPERATION = WS VARNAME WS ( '=' | '+=' ) WS EXPRESSION
-SUBTREE = WS VARNAME WS '{' BLOCK WS '}'
-CONDITION = WS 'if' FORMULA WS '{' BLOCK WS '}'
+    OPERATION = WS VARNAME WS ( '=' | '+=' ) WS EXPRESSION
+    SUBTREE = WS VARNAME WS '{' BLOCK WS '}'
+    CONDITION = WS 'if' FORMULA WS '{' BLOCK WS '}'
 
 
-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 
+    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 
 
 
-NOTE: ';' or '\n' is currently required even after CONDITION and SUBTREE block 
-  TODO: change to OPERATION only
-NOTE: Formula may contain additional/extra parentheses
+    EXPRESSION = '"' ( ECHAR | '{' VARNAME '}' )* '"' | re"'[^'\n]*'" | VARNAME
+    ECHAR = re('([^\{}]|\\|\{|\}|\\n)*')
+    VARNAME = re('[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)*')
 
 
-EXPRESSION = '"' ( ECHAR | '{' VARNAME '}' )* '"' | re"'[^'\\n]*'" | VARNAME
-ECHAR = re('([^\\{}]|\\\\|\\{|\\}|\\n)*')
-VARNAME = re('[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)*')
+.. 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
 """
 
 import re, types, itertools, logging as log
 """
 
 import re, types, itertools, logging as log
@@ -242,8 +236,10 @@ class ConfigParser(object):
       op = 'SET'
     elif self.nexts(self.c_append):
       op = 'APPEND'
       op = 'SET'
     elif self.nexts(self.c_append):
       op = 'APPEND'
+    elif self.eof():
+      self.syntax_error('Unexpected end of file.')
     else:
     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)
     self.p_WS()
     exp = self.p_EXPRESSION()
     vname = (self.prefix+self.c_varname_sep+varname).lstrip(self.c_varname_sep)