lep_parser.html


001| '''\
001| Logical Expression Recursive Descent Parser.
001| 
001| BNF:
001|     expr   = term
001|            = term oper term
001| 
001|     term   = name
001|            = '(' expr ')'
001|            = not term
001| 
001|     name   = alpha
001|            = alpha alnums
001|     alnums = alnum
001|            = alnum alnums
001|     alnum  = alpha
001|            = number
001| 
001|     oper   = '|'
001|            = '&'
001|            = '^'
001|     not    = 'not'
001|     alpha  = 'A'..'Z'
001|            = 'a'..'z'
001|     number = '0'..'9'
001| 
001| '''

002| ####################################################################################################
003| from lep_objects import *
004| ####################################################################################################
005| DEBUG = 0
006| 
007| 
008| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
009| class source (object):
010|     '''Logical expression source text class.'''
011|     lib = {}
012| 
013|     def __init__ (self, t):
014|         self.text = t
015|         self.index = 0
016| 
017|     def reset (self):
018|         self.index = 0
019|         return self()
020| 
021|     def incr (self):
022|         if len(self.text) <= self.index:
023|             return self()
024|         self.index += 1
025|         return self()
026| 
027|     def skip_ws (self):
028|         ch = self()
029|         while ch.isspace():
030|             ch = self.incr()
031|         return self()
032| 
033|     def word (self, w):
034|         n = len(w)
035|         ix = self.index
036|         w1 = self.text[ix:ix+n].upper()
037|         w2 = w.upper()
038|         if w1 == w2:
039|             self.index += n
040|             return True
041|         return False
042| 
043|     def __call__ (self):
044|         ix = self.index
045|         ch = self.text[ix:ix+1]
046|         return ch
047| 
048|     def __len__ (self):
049|         return len(self.text)
050| 
051|     def __str__ (self):
052|         return self.text
053| 
054|     def __repr__ (self):
055|         s = '"%s"[%d]'
056|         t = (self.text, self.index)
057|         return s % t
058| 
059| 
060| 
061| ##================================================================================================##
062| def parse_operator (src):
063|     '''Test: Operator.'''
064|     ch = src.skip_ws()
065| 
066|     # Operator,... give me in-for-mation,...
067|     if ch == '|':
068|         src.incr()
069|         return oper_or()
070|     if ch == '&':
071|         src.incr()
072|         return oper_and()
073|     if ch == '^':
074|         src.incr()
075|         return oper_xor()
076| 
077|     # If it's OR...
078|     if src.word('or'):
079|         if DEBUG: print('oper.OR:')
080|         return oper_or()
081|     # If it's AND...
082|     if src.word('and'):
083|         if DEBUG: print('oper.AND:')
084|         return oper_and()
085|     # If it's XOR...
086|     if src.word('xor'):
087|         if DEBUG: print('oper.XOR:')
088|         return oper_xor()
089|     # If it's NOR...
090|     if src.word('nor'):
091|         if DEBUG: print('oper.NOR:')
092|         return oper_nor()
093|     # If it's NAND...
094|     if src.word('nand'):
095|         if DEBUG: print('oper.NAND:')
096|         return oper_nand()
097| 
098|     # Unknown, so an error...
099|     raise SyntaxError('Operator expected! [@%d]' % src.index)
100| 
101| 
102| ##================================================================================================##
103| def parse_term (src):
104|     '''Test: Term.'''
105|     ch = src.skip_ws()
106| 
107|     # If it's an open-parenthesis, this is an enclosed term...
108|     if ch == '(':
109|         src.incr()
110|         expr = parse_expression(src)
111|         ch = src.skip_ws()
112|         if ch != ')':
113|             raise SyntaxError('Close parenthesis expected! [@%d]' % src.index)
114|         src.incr()
115|         if DEBUG: print('term.expr: %s' % expr)
116|         return expr
117| 
118|     # If it's a NOT...
119|     if src.word('not'):
120|         if DEBUG: print('expr.NOT:')
121|         # Create a NOT operator...
122|         op = oper_not()
123| 
124|         # Get the term...
125|         t1 = parse_term(src)
126|         if DEBUG: print('expr.t1: %s' % t1)
127| 
128|         # Return a NOT expression...
129|         return expression(op, t1, None)
130| 
131|     # If it's an alpha, this is a name...
132|     if ch.isalpha():
133|         name = ch
134|         ch = src.incr()
135|         while ch.isalnum():
136|             name += ch
137|             ch = src.incr()
138|         if name in src.lib:
139|             if DEBUG: print('term.name: %s (exists)' % name)
140|             return src.lib[name]
141|         # Add new term to library...
142|         if DEBUG: print('term.name: %s (NEW)' % name)
143|         t = term(name)
144|         src.lib[name] = t
145|         return t
146| 
147|     # Unknown, so an error...
148|     raise SyntaxError('Term expected! [@%d]' % src.index)
149| 
150| 
151| ##================================================================================================##
152| def parse_expression (src):
153|     '''Test: Expression.'''
154|     src.skip_ws()
155| 
156|     # Get term #1...
157|     t1 = parse_term(src)
158|     if DEBUG: print('expr.t1: %s' % t1)
159| 
160|     # Get operator (or fail if just a term)...
161|     try:
162|         op = parse_operator(src)
163|         if DEBUG: print('expr.op: %s' % op)
164|     except SyntaxError:
165|         # Return a term...
166|         return t1
167| 
168|     # Get term #2...
169|     t2 = parse_term(src)
170|     if DEBUG: print('expr.t2: %s' % t2)
171| 
172|     # Return an expression...
173|     return expression(op, t1, t2)
174| 
175| 
176| 
177| ####################################################################################################
178| '''eof'''
179|