]> thyrsus.com Git - mad.git/commitdiff
Add line number and sourcefile in ERROR RETURN message.
authorEric S. Raymond <esr@thyrsus,com>
Wed, 18 Feb 2004 21:28:01 +0000 (21:28 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Wed, 18 Feb 2004 21:28:01 +0000 (21:28 +0000)
mad

diff --git a/mad b/mad
index 7e2c92aba13f4750e0de56b17e39111c49ccfc10..af12b41ad75a7287a7d5f9fa2b2d588f4280bcac 100755 (executable)
--- a/mad
+++ b/mad
@@ -94,7 +94,9 @@ def t_INTNUM(t):
 t_ignore = ' \n\t'
 
 def t_error(t):
-    sys.stderr.write('"%s", line %d: illegal character %s\n' % (sourcefile, t.lineno, `t.value[0]`))
+    global sourceline
+    sourceline = t.lineno
+    nonfatal('illegal character %s' % `t.value[0]`)
     t.skip(1)
     
 # Build the lexer
@@ -300,8 +302,9 @@ def p_range(t):
     'range : NAME application DOTRANGE NAME application'
     t[0] = ('range', t[1], t[2][0], t[5][0])
     if t[1] != t[4]:
-        sys.stderr.write("\"%s\", %d: variable names in range don't match!" % (sourcefile, t.lineno(1)))
-        sys.exit(1)
+        global sourceline
+        sourceline = t.lineno(1)
+        nonfatal("variable names in range don't match.")
 
 ## R
 def p_remark1(t):
@@ -495,7 +498,9 @@ def p_expression_name(t):
 
 def p_error(t):
     "Gets fed an error token."
-    sys.stderr.write('"%s", line %d: syntax error at "%s"\n' % (sourcefile, t.lineno, t.value))
+    global sourceline
+    sourceline = t.lineno
+    nonfatal('syntax error on token "%s"' % t.value)
 
 import yacc
 yacc.yacc()
@@ -672,7 +677,7 @@ def analyze():
                 else:
                     nonfatal('no EXTERNAL declaration matches this ENTRY')
     except:
-        sys.stderr.write("mad: internal error while analyzing code for line %d\n" % line)
+        nonfatal("internal error while analyzing code")
         (exc_type, exc_value, exc_traceback) = sys.exc_info()
         raise exc_type, exc_value, exc_traceback
 
@@ -715,7 +720,7 @@ def mode(t):
         if left == 'int' or right == 'int':
             #print "<-mode of binary operand"
             return 'int'
-        sys.stderr.write("mad: internal error while computing mode of %s\n" % `t`)
+        nonfatal("internal error while computing mode of %s\n" % `t`)
         sys.exit(1)
 
 def prinC1(t):
@@ -811,8 +816,7 @@ def fmt_convert(str):
             while str[i] != ',':
                 i += 1
             if i <= 0:
-                sys.stderr.write('"%s", line %d: invalid spacer in %s\n' % (sourcefile, sourceline, str))
-                sys.exit(1)
+                nonfatal('invalid spacer in %s\n' % str)
             count = int(str[1:i])
             res += ' ' * count
             str = str[i:]
@@ -862,8 +866,7 @@ def fmt_convert(str):
             str = str[i:]
             types += ['int'] * count
         else:
-            sys.stderr.write('"%s", line %d: invalid field code %s at %d in format string %s!\n' % (sourcefile, sourceline, `str[0]`, len(fmt)-len(str), `fmt`))
-            sys.exit(1)
+            nonfatal('invalid field code %s at %d in format string %s!\n' % (`str[0]`, len(fmt)-len(str), `fmt`))
     #print "<-fmt_convert() = %s" % res
     return (res, types)
 
@@ -882,7 +885,7 @@ def lptcodes(res):
     elif res[0] == '4':                # Skip to next quarter page
         res = res + r"\v" + res[1:]
     else:                              # Anything else falls through 
-        sys.stderr.write('unknown print code %s ignored\n' % res[0])
+        nonfatal('unknown print code %s ignored\n' % res[0])
         res = res[1:]
     res = res + '\\n'
     return res
@@ -928,11 +931,9 @@ def print_C_statement(statement):
             if rep.endswith(", "):
                 rep = rep[:-2]
         rep  = rep + ") {"
-        #if defaulted:
-        #    sys.stderr.write("warning: defaulted base type for %s\n" % ", ".join(defaulted))
         return rep
     elif statement[0] == 'error_return':
-        return 'puts("ERROR RETURN"); exit(1);'
+        return 'puts("ERROR RETURN from line %s:%d"); exit(1);' % (sourcefile,sourceline)
     elif statement[0] == 'execute':
         return print_C_expr(statement[1]) + ";"
     elif statement[0] == 'external_call':
@@ -984,8 +985,7 @@ def print_C_statement(statement):
             elif type(ispec)==type(()) and ispec[0]=='range' and itype=='char':
                 inlist.append(print_C_expr(ispec))
             else:
-                sys.stderr.write("unsupported type in READ FORMAT\n")
-                sys.exit(1)
+                nonfatal("unsupported type in READ FORMAT")
         return 'scanf("' + fmt + '", ' + ', '.join(inlist) + ');'
     elif statement[0] == 'remark':
         if len(statement) == 1:
@@ -1050,9 +1050,9 @@ def generate_C_code(out):
     out.write("\n")
 
     indent = 0
-    global sourceline
+    global sourcefile, sourceline
     scopes = []                # THROUGH scopes that are pending
-    for (file, sourceline, label, statement) in deck:
+    for (sourcefile, sourceline, label, statement) in deck:
         if label in targets:
             out.write("  " + label+':\n')
         if statement[0] in ('for', 'map'):
@@ -1060,7 +1060,7 @@ def generate_C_code(out):
         try:
             rep = print_C_statement(statement)
         except:
-            sys.stderr.write("mad: internal error while generating code for line %d\n" % sourceline)
+            nonfatal("internal error while generating code")
             (exc_type, exc_value, exc_traceback) = sys.exc_info()
             raise exc_type, exc_value, exc_traceback
         if rep: