tinyevil

Untitled

Jun 27th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. from jf.parser.lexer_base import LexerBase, LexerGrammar, LexerError
  2. from enum import Enum
  3.  
  4.  
  5. class TokenType(Enum):
  6.     Identifier = 0
  7.     IntLiteral = 1
  8.     FloatLiteral = 2
  9.  
  10.     K_Namespace = 3
  11.  
  12.     EOF = -1
  13.  
  14.  
  15. grammar = LexerGrammar(
  16.     [
  17.         ("namespace", TokenType.K_Namespace),
  18.  
  19.         ("[a-zA-Z_][a-zA-Z_0-9]*", TokenType.Identifier),
  20.  
  21.         ("0|[1-9][0-9]*([.][0-9]+)?((e|E)[+-]?[0-9]+)?", TokenType.FloatLiteral),
  22.         ("0|[1-9][0-9]*", TokenType.IntLiteral),
  23.     ],
  24.     TokenType.EOF
  25. )
  26.  
  27.  
  28. def tokenize(source):
  29.     lexer = LexerBase(grammar)
  30.     lexer.set_input(source)
  31.     yield from lexer.tokens()
Add Comment
Please, Sign In to add comment