2000-02-26 10:24:46 +00:00
|
|
|
import sys,re
|
|
|
|
|
import PyLR
|
|
|
|
|
|
|
|
|
|
def _intfunc(m):
|
|
|
|
|
return int(m.group(0))
|
|
|
|
|
|
|
|
|
|
def _realfunc(m):
|
|
|
|
|
return float(m.group(0))
|
|
|
|
|
|
|
|
|
|
class GMLLexer(PyLR.Lexer):
|
|
|
|
|
"""The GML lexical scanner."""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
PyLR.Lexer.__init__(self)
|
|
|
|
|
self.addpat(r"[-+]?(\d+\.\d*|\d*\.\d+)([Ee][-+]?\d+)?",
|
|
|
|
|
"REAL", _realfunc)
|
2000-02-28 13:44:24 +00:00
|
|
|
self.addpat(r"[-+]?\d+", "INTEGER", _intfunc)
|
2000-02-26 10:24:46 +00:00
|
|
|
self.addpat(r"\[", "LSQB")
|
|
|
|
|
self.addpat(r"\]", "RSQB")
|
|
|
|
|
self.addpat(r'"([^&"]+|&[a-zA-Z]+;)*"', "STRING")
|
|
|
|
|
self.addpat(r"[a-zA-Z][a-zA-Z0-9]*", "KEY")
|
2000-02-27 00:43:20 +00:00
|
|
|
self.addpat(r"#[^\n]*", "", None, 1)
|
|
|
|
|
self.addpat(r"\s+", "", None, 1)
|
2000-02-26 10:24:46 +00:00
|
|
|
|
|
|
|
|
def _test():
|
|
|
|
|
gmltest = """# a graph example
|
|
|
|
|
graph [ # comment at end of line
|
|
|
|
|
node [
|
|
|
|
|
real1 1.e3
|
|
|
|
|
real2 .01
|
|
|
|
|
int1 00050
|
|
|
|
|
label "Wallerfang&Ballern"
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
"""
|
|
|
|
|
# create the lexer
|
|
|
|
|
lexer = GMLLexer()
|
|
|
|
|
lexer.settext(gmltest)
|
2000-02-27 00:43:20 +00:00
|
|
|
print lexer.getTokenList()
|
2000-02-26 10:24:46 +00:00
|
|
|
tok=1
|
|
|
|
|
while tok:
|
|
|
|
|
tok, val = lexer.scan(1)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
_test()
|