diff options
Diffstat (limited to 'lib/lex.py')
-rw-r--r-- | lib/lex.py | 104 |
1 files changed, 57 insertions, 47 deletions
@@ -3,34 +3,44 @@ from .sly import Lexer, Parser class TimedWordLexer(Lexer): tokens = {LPAREN, RPAREN, IDENTIFIER, NUMBER, ARGSEP, FUNCTIONSEP} - ignore = ' \t' + ignore = " \t" - LPAREN = r'\(' - RPAREN = r'\)' - IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*' - NUMBER = r'[0-9e.]+' - ARGSEP = r',' - FUNCTIONSEP = r';' + LPAREN = r"\(" + RPAREN = r"\)" + IDENTIFIER = r"[a-zA-Z_][a-zA-Z0-9_]*" + NUMBER = r"[0-9e.]+" + ARGSEP = r"," + FUNCTIONSEP = r";" class TimedSequenceLexer(Lexer): - tokens = {LPAREN, RPAREN, LBRACE, RBRACE, CYCLE, IDENTIFIER, NUMBER, ARGSEP, FUNCTIONSEP} - ignore = ' \t' - - LPAREN = r'\(' - RPAREN = r'\)' - LBRACE = r'\{' - RBRACE = r'\}' - CYCLE = r'cycle' - IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*' - NUMBER = r'[0-9e.]+' - ARGSEP = r',' - FUNCTIONSEP = r';' + tokens = { + LPAREN, + RPAREN, + LBRACE, + RBRACE, + CYCLE, + IDENTIFIER, + NUMBER, + ARGSEP, + FUNCTIONSEP, + } + ignore = " \t" + + LPAREN = r"\(" + RPAREN = r"\)" + LBRACE = r"\{" + RBRACE = r"\}" + CYCLE = r"cycle" + IDENTIFIER = r"[a-zA-Z_][a-zA-Z0-9_]*" + NUMBER = r"[0-9e.]+" + ARGSEP = r"," + FUNCTIONSEP = r";" def error(self, t): print("Illegal character '%s'" % t.value[0]) - if t.value[0] == '{' and t.value.find('}'): - self.index += 1 + t.value.find('}') + if t.value[0] == "{" and t.value.find("}"): + self.index += 1 + t.value.find("}") else: self.index += 1 @@ -38,39 +48,39 @@ class TimedSequenceLexer(Lexer): class TimedWordParser(Parser): tokens = TimedWordLexer.tokens - @_('timedSymbol FUNCTIONSEP timedWord') + @_("timedSymbol FUNCTIONSEP timedWord") def timedWord(self, p): ret = [p.timedSymbol] ret.extend(p.timedWord) return ret - @_('timedSymbol FUNCTIONSEP', 'timedSymbol') + @_("timedSymbol FUNCTIONSEP", "timedSymbol") def timedWord(self, p): return [p.timedSymbol] - @_('IDENTIFIER', 'IDENTIFIER LPAREN RPAREN') + @_("IDENTIFIER", "IDENTIFIER LPAREN RPAREN") def timedSymbol(self, p): return (p.IDENTIFIER,) - @_('IDENTIFIER LPAREN args RPAREN') + @_("IDENTIFIER LPAREN args RPAREN") def timedSymbol(self, p): return (p.IDENTIFIER, *p.args) - @_('arg ARGSEP args') + @_("arg ARGSEP args") def args(self, p): ret = [p.arg] ret.extend(p.args) return ret - @_('arg') + @_("arg") def args(self, p): return [p.arg] - @_('NUMBER') + @_("NUMBER") def arg(self, p): return float(p.NUMBER) - @_('IDENTIFIER') + @_("IDENTIFIER") def arg(self, p): return p.IDENTIFIER @@ -78,66 +88,66 @@ class TimedWordParser(Parser): class TimedSequenceParser(Parser): tokens = TimedSequenceLexer.tokens - @_('timedSequenceL', 'timedSequenceW') + @_("timedSequenceL", "timedSequenceW") def timedSequence(self, p): return p[0] - @_('loop') + @_("loop") def timedSequenceL(self, p): return [p.loop] - @_('loop timedSequenceW') + @_("loop timedSequenceW") def timedSequenceL(self, p): ret = [p.loop] ret.extend(p.timedSequenceW) return ret - @_('timedWord') + @_("timedWord") def timedSequenceW(self, p): return [p.timedWord] - @_('timedWord timedSequenceL') + @_("timedWord timedSequenceL") def timedSequenceW(self, p): ret = [p.timedWord] ret.extend(p.timedSequenceL) return ret - @_('timedSymbol FUNCTIONSEP timedWord') + @_("timedSymbol FUNCTIONSEP timedWord") def timedWord(self, p): p.timedWord.word.insert(0, p.timedSymbol) return p.timedWord - @_('timedSymbol FUNCTIONSEP') + @_("timedSymbol FUNCTIONSEP") def timedWord(self, p): return TimedWord(word=[p.timedSymbol]) - @_('CYCLE LPAREN IDENTIFIER RPAREN LBRACE timedWord RBRACE') + @_("CYCLE LPAREN IDENTIFIER RPAREN LBRACE timedWord RBRACE") def loop(self, p): return Workload(p.IDENTIFIER, p.timedWord) - @_('IDENTIFIER', 'IDENTIFIER LPAREN RPAREN') + @_("IDENTIFIER", "IDENTIFIER LPAREN RPAREN") def timedSymbol(self, p): return (p.IDENTIFIER,) - @_('IDENTIFIER LPAREN args RPAREN') + @_("IDENTIFIER LPAREN args RPAREN") def timedSymbol(self, p): return (p.IDENTIFIER, *p.args) - @_('arg ARGSEP args') + @_("arg ARGSEP args") def args(self, p): ret = [p.arg] ret.extend(p.args) return ret - @_('arg') + @_("arg") def args(self, p): return [p.arg] - @_('NUMBER') + @_("NUMBER") def arg(self, p): return float(p.NUMBER) - @_('IDENTIFIER') + @_("IDENTIFIER") def arg(self, p): return p.IDENTIFIER @@ -165,8 +175,8 @@ class TimedWord: def __repr__(self): ret = list() for symbol in self.word: - ret.append('{}({})'.format(symbol[0], ', '.join(map(str, symbol[1:])))) - return 'TimedWord<"{}">'.format('; '.join(ret)) + ret.append("{}({})".format(symbol[0], ", ".join(map(str, symbol[1:])))) + return 'TimedWord<"{}">'.format("; ".join(ret)) class Workload: @@ -196,5 +206,5 @@ class TimedSequence: def __repr__(self): ret = list() for symbol in self.seq: - ret.append('{}'.format(symbol)) - return 'TimedSequence(seq=[{}])'.format(', '.join(ret)) + ret.append("{}".format(symbol)) + return "TimedSequence(seq=[{}])".format(", ".join(ret)) |