Commits

Ronny Pfannschmidt committed 09244f7

more tests, resuffle the parser

  • Participants
  • Parent commits f0e5d44

Comments (0)

Files changed (2)

File testing/test_parse_command.py

 from whenever import parse_command
 commands = [
+    '1;',
+    '1#2;',
+    'print(42);',
+    '4,-3,7;',
+    'print("a");',
+    'defer (3 || 6) 1,3;',
     'defer (1) again (2) 3;',
+    'defer (3 || N(1)<=N(2) || N(7)>99) 2#N(1),3,7;',
     'again (1) defer (3 || N(1)<=N(2) || N(7)>99) 2#N(1),3,7;',
-    'defer (3 || N(1)<=N(2))',
+    'defer (3 || N(1)<=N(2));',
+    'print(N(1)+N(2));',
+    'defer (5) print(N(1)+N(2));',
+    'defer (5) print(1+1);',
+    'defer (N(1) + N(3));',
 ]
 
 

File whenever/__init__.py

     fp = open_file_as_stream(filename)
     while True:
         line = fp.readline()
+        if len(line) == 0:
+            break
         number, commands = parseline(line)
         assert number not in result
         result[number] = commands
     except Exception, e:
         print e.args[0], e.nice_error_message()
         print commands
+        print ' '*e.args[0].i + '^'
         raise
 
 regexs, rules, ToAST = parse_ebnf(
 """
 IGNORE: " ";
 DECIMAL: "0|[1-9][0-9]*";
+STRING: "\\"[^\\\\"]*\\"";
 
-command:  op ";";
-op: action+ statements | statements | action+;
+RPAR: "\)";
+LPAR: "\(";
+PLUS: "\+";
+STAR: "\*";
+FUNC: "N";
 
+LT: "<";
+GT: ">";
+LTE: "<=";
+GTE: ">=";
 
-number: DECIMAL;
+SHARP: "#";
+COMMA: ",";
+SEMICOLON: ";";
 
+OR: "\|\|";
+AND: "&&";
 
-math: number;
+command:  op SEMICOLON;
+op: action+ statements | action+ | statements;
 
-function: "N" "(" expr ")";
 
 
+integer: DECIMAL | "-" DECIMAL;
+number: integer| function | STRING;
 
+statement: integer [SHARP] expr | integer;
+statements: (statement [COMMA])* statement;
 
-expr: function | math;
 
 
-chain: "||" | "&&";
-compare: ">" | ">=" | "<" | "<=";
-bool: expr compare expr | expr;
-booleans: (bool chain)* bool;
+function: "N" [LPAR] expr [RPAR];
 
+addition: (number [PLUS])* number;
+expr: addition|number;
 
-statement: expr "#" expr | expr;
-statements: (statement ",")* statement;
+comparisation: LT | GT | LTE | GTE;
+compare: expr comparisation expr;
 
-action_name: "print" | "defer" | "again" | "forget";
-action: action_name "(" booleans ")";
+
+bool: compare | expr;
+chain: AND |OR;
+boolean: (bool chain)* bool;
+
+
+ACTION: "defer|again|forget";
+PRINT: "print";
+action: ACTION [LPAR] boolean [RPAR] | PRINT [LPAR] expr [RPAR];
 
 """)
 
+
 parse_command = make_parse_function(regexs, rules)