]> thyrsus.com Git - mad.git/commitdiff
We can parse DIMENSION statements.
authorEric S. Raymond <esr@thyrsus,com>
Wed, 14 Jan 2004 08:07:48 +0000 (08:07 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Wed, 14 Jan 2004 08:07:48 +0000 (08:07 +0000)
mad

diff --git a/mad b/mad
index ab7332529f83b70ebc0e1af0d1c37110a8f96e50..7c6d8f843b553b6217500908d5dc5f1bb2742431 100755 (executable)
--- a/mad
+++ b/mad
@@ -1,7 +1,9 @@
 #!/usr/bin/env python
 
-tokens = (
-    'TAB',
+reserved = ('DIMENSION',)
+
+tokens = reserved + (
+    'TAB', 'NEWLINE',
     'COMMA',
     'NAME','INTEGER', 'FLOAT',
     'LPAREN','RPAREN',
@@ -14,6 +16,7 @@ tokens = (
 # Tokens
 
 t_TAB     = r'\t'
+t_NEWLINE = r'\n'
 t_COMMA   = r'\,'
 t_ABS     = r'\.ABS\.'
 t_EXP     = r'\.P\.'
@@ -35,18 +38,22 @@ t_DIVIDE  = r'/'
 t_EQUALS  = r'='
 t_LPAREN  = r'\('
 t_RPAREN  = r'\)'
-t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'
 t_FLOAT   = r'\d+\.\d*'
 t_INTEGER = r'\d+'
 
+reserved_map = { }
+for r in reserved:
+    reserved_map[r] = r
+
+def t_NAME(t):
+    r'[a-zA-Z_][a-zA-Z0-9_]*'
+    t.type = reserved_map.get(t.value, "NAME")
+    return t
+
 t_ignore = " "
 
-def t_newline(t):
-    r'\n+'
-    t.lineno += t.value.count("\n")
-    
 def t_error(t):
-    print "Illegal character '%s'" % t.value[0]
+    print "Illegal character %s" % `t.value[0]`
     t.skip(1)
     
 # Build the lexer
@@ -78,16 +85,28 @@ def p_program(t):
                | program card'''
 
 def p_card(t):
-    'card : TAB statement' 
+    'card : TAB statement NEWLINE' 
     cards.append((None, t[2]))
 
 def p_labeled_card(t):
-    'card : NAME TAB statement'
+    'card : NAME TAB statement NEWLINE'
     cards.append((t[1], t[3]))
 
 # Statement level
 
-def p_statement_assign(t):
+## DIMENSION
+def p_statement_dimension(t):
+    'statement : DIMENSION dimension_list'
+    t[0] = ('dimension', t[1], t[2])
+def p_dimension_list(t):
+    'dimension_list : NAME application'
+    t[0] = [[t[1], tuple(t[2])]]
+def p_dimension_list_continue(t):
+    'dimension_list : dimension_list COMMA NAME application'
+    t[0] = t[1] + [[t[3], tuple(t[4])]]
+
+## Assignment
+def p_statement_substitution(t):
     'statement : NAME EQUALS expression'
     t[0] = ('substitution', t[1], t[3])
 
@@ -173,5 +192,5 @@ yacc.yacc()
 
 import sys
 
-yacc.parse(sys.stdin.read())
+yacc.parse(sys.stdin.read(), debug=2)
 print cards