summaryrefslogtreecommitdiff
path: root/lib/lex.py
diff options
context:
space:
mode:
authorDaniel Friesel <daniel.friesel@uos.de>2019-12-12 17:34:01 +0100
committerDaniel Friesel <daniel.friesel@uos.de>2019-12-12 17:34:01 +0100
commitde382bd34807a32af948a017164f0fb91cb2e6cb (patch)
treec4b0f968637c2e4ebf120373cc42a27cfea3bc47 /lib/lex.py
parentff6fc70456354b23663bee037c5e737a2b437ca8 (diff)
lexer and parser for basic timed word strings
Diffstat (limited to 'lib/lex.py')
-rw-r--r--lib/lex.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/lex.py b/lib/lex.py
new file mode 100644
index 0000000..c0323fa
--- /dev/null
+++ b/lib/lex.py
@@ -0,0 +1,62 @@
+from sly import Lexer, Parser
+
+
+class TimedWordLexer(Lexer):
+ tokens = {LPAREN, RPAREN, IDENTIFIER, NUMBER, ARGSEP, FUNCTIONSEP}
+ ignore = ' \t'
+
+ LPAREN = r'\('
+ RPAREN = r'\)'
+ IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*'
+ NUMBER = r'[0-9e.]+'
+ ARGSEP = r','
+ FUNCTIONSEP = r';'
+
+
+class TimedWordParser(Parser):
+ tokens = TimedWordLexer.tokens
+
+ @_('timedSymbol FUNCTIONSEP timedWord')
+ def timedWord(self, p):
+ ret = [p.timedSymbol]
+ ret.extend(p.timedWord)
+ return ret
+
+ @_('timedSymbol FUNCTIONSEP', 'timedSymbol')
+ def timedWord(self, p):
+ return [p.timedSymbol]
+
+ @_('IDENTIFIER', 'IDENTIFIER LPAREN RPAREN')
+ def timedSymbol(self, p):
+ return (p.IDENTIFIER,)
+
+ @_('IDENTIFIER LPAREN args RPAREN')
+ def timedSymbol(self, p):
+ return (p.IDENTIFIER, *p.args)
+
+ @_('arg ARGSEP args')
+ def args(self, p):
+ ret = [p.arg]
+ ret.extend(p.args)
+ return ret
+
+ @_('arg')
+ def args(self, p):
+ return [p.arg]
+
+ @_('NUMBER')
+ def arg(self, p):
+ return [float(p.NUMBER)]
+
+ @_('IDENTIFIER')
+ def arg(self, p):
+ return [p.IDENTIFIER]
+
+
+if __name__ == '__main__':
+ data = 'init(); sleep(12345); foo(_, 7);'
+ lexer = TimedWordLexer()
+ parser = TimedWordParser()
+ for tok in lexer.tokenize(data):
+ print('type={}, value={}'.format(tok.type, tok.value))
+ print(parser.parse(lexer.tokenize(data)))