22from copy import deepcopy
33from auxml .util import *
44from auxml .err import SyntaxErrorAuXML
5+ import auxml .parser as parser
6+
57'''
68<define-macro name="blue"><span style="color: #00f"><b><contents/></b></span></define-macro>
79'''
@@ -27,7 +29,22 @@ def append_tail(el, s):
2729class MacroDef ():
2830 def __init__ (self , el ):
2931 self .el = el
32+ self .check_for_name ()
3033 self .name = el .get ("name" )
34+
35+ def check_for_name (self ):
36+ if self .el .get ("name" ) is None :
37+ fileinfo = parser .el_location_info (self .el )
38+ msg = f'''
39+ Encountered macro definition with missing `name` attribute
40+ { fileinfo }
41+
42+ Suggestion: change the macro definition include a name attribute:
43+
44+ <define-macro name="..."> ... </define-macro>
45+ ^^^^
46+ '''
47+ raise SyntaxErrorAuXML (msg )
3148
3249 def replace_one_var (self , el , varname , valuem ):
3350 pass
@@ -39,12 +56,15 @@ def get_body(self):
3956 # the following line assumes the macro body only has one element.
4057 # TODO let macros definition have text and elements.
4158 cs = self .el .getchildren ()
59+ fileinfo = parser .el_location_info (self .el )
4260
4361 if len (cs ) == 0 :
44- raise SyntaxErrorAuXML (f"Encountered empty macro body in macro definition: `{ self .name } `" )
62+ # maybe this should be a warning?
63+ msg = f"Encountered empty macro body in macro definition: `{ self .name } `, { fileinfo } "
64+ raise SyntaxErrorAuXML (msg )
4565
4666 if len (cs ) > 1 :
47- msg = "macro definitions may not yet have more than one element"
67+ msg = f"Macro definitions may not yet have more than one element, { fileinfo } "
4868 raise SyntaxErrorAuXML (msg )
4969
5070 return deepcopy (self .el .getchildren ()[0 ])
@@ -83,8 +103,9 @@ def attr_vars(self):
83103
84104 def ensure_attrs_match (self , mcall ):
85105 for var in self .attr_vars ():
86- if not mcall .contains_attr (var ):
87- raise Exception (f"Macro call: { mcall .name ()} on line ... must have attribute: { var } " )
106+ if not mcall .contains_attr (var ):
107+ info = mcall .fileinfo ()
108+ raise SyntaxErrorAuXML (f"Macro call: { mcall .name ()} on line ... must have attribute: { var } { info } " )
88109
89110
90111 def replace_one_content (self , mcall , con ):
@@ -196,6 +217,9 @@ def __init__(self, el):
196217 self .el = el
197218 self .counter = MacroCall .counter
198219 MacroCall .counter += 1
220+
221+ def fileinfo (self ):
222+ return parser .el_location_info (self .el )
199223
200224 def unique_id (self ):
201225 return f"{ self .name ()} -{ self .counter } "
0 commit comments