]> git.saurik.com Git - cycript.git/blob - Parser.py
Improved lexical syntax errors and added not-isatty detection for selecting the parse...
[cycript.git] / Parser.py
1 #!/usr/bin/python
2
3 import sys
4
5 lines = sys.stdin.read().rstrip('\n').split('\n')
6
7 def data(line):
8 name = line[0].replace('&', 'Ampersand').replace('^', 'Carrot').replace('=', 'Equal').replace('!', 'Exclamation').replace('-', 'Hyphen').replace('<', 'Left').replace('%', 'Percent').replace('.', 'Period').replace('|', 'Pipe').replace('+', 'Plus').replace('>', 'Right').replace('/', 'Slash').replace('*', 'Star').replace('~', 'Tilde')
9 text = line[0].lower()
10 word = text[0].isalpha()
11 prefix = None if line[1] == '-' else line[1]
12 assign = None if len(line) < 3 or line[2] != 'A' else '' if len(line) < 4 else line[3]
13 infix = None if len(line) < 3 or line[2] != 'R' else line[3]
14 precedence = line[4] if infix != None and len(line) > 4 else None
15 postfix = infix if infix != None and precedence == None else None
16 if postfix != None:
17 infix = None
18 return name, text, word, prefix, assign, infix, precedence, postfix
19
20 def express(expression, type, args, call):
21 print 'struct CYExpression%(expression)s :' % locals()
22 print ' CYExpression%(type)s' % locals()
23 print '{'
24 print ' CYExpression%(expression)s(%(args)s) :' % locals()
25 print ' CYExpression%(type)s(%(call)s)' % locals()
26 print ' {'
27 print ' }'
28 print '};'
29 print
30
31 for line in lines:
32 line = line.split()
33 name, text, word, prefix, assign, infix, precedence, postfix = data(line)
34
35 print 'struct CYToken%(name)s :' % locals()
36 if prefix != None:
37 print ' CYTokenPrefix,'
38 if infix != None:
39 print ' CYTokenInfix,'
40 if postfix != None:
41 print ' CYTokenPostfix,'
42 if assign != None:
43 print ' CYTokenAssignment,'
44 if word:
45 print ' CYTokenWord,'
46 print ' virtual CYToken'
47 print '{'
48 print ' virtual const char *Text() const {'
49 print ' return "%(text)s";' % locals()
50 print ' }'
51 if precedence != None or prefix != None or assign != None or infix != None:
52 print
53 if precedence != None:
54 print ' virtual unsigned Precedence() const {'
55 print ' return %(precedence)s;' % locals()
56 print ' }'
57 print
58 if prefix != None:
59 print ' virtual CYExpression *PrefixExpression(apr_pool_t *pool, CYExpression *rhs) const;'
60 if infix != None:
61 print ' virtual CYExpression *InfixExpression(apr_pool_t *pool, CYExpression *lhs, CYExpression *rhs) const;'
62 if postfix != None:
63 print ' virtual CYExpression *PostfixExpression(apr_pool_t *pool, CYExpression *lhs) const;'
64 if assign != None:
65 print ' virtual CYExpression *AssignmentExpression(apr_pool_t *pool, CYExpression *lhs, CYExpression *rhs) const;'
66 print '};'
67 print
68 if prefix != None:
69 express(prefix, 'Prefix', 'CYExpression *rhs', 'rhs')
70 if infix != None:
71 express(infix, 'Infix', 'CYExpression *lhs, CYExpression *rhs', 'lhs, rhs')
72 if postfix != None:
73 express(postfix, 'Postfix', 'CYExpression *lhs', 'lhs')
74 if assign != None:
75 express('Assign' + assign, 'Assignment', 'CYExpression *lhs, CYExpression *rhs', 'lhs, rhs')
76
77 for line in lines:
78 line = line.split()
79 name, text, word, prefix, assign, infix, precedence, postfix = data(line)
80
81 if prefix != None:
82 print 'CYExpression *CYToken%(name)s::PrefixExpression(apr_pool_t *pool, CYExpression *rhs) const {' % locals()
83 print ' return new(pool) CYExpression%(prefix)s(rhs);' % locals()
84 print '}'
85 print
86 if infix != None:
87 print 'CYExpression *CYToken%(name)s::InfixExpression(apr_pool_t *pool, CYExpression *lhs, CYExpression *rhs) const {' % locals()
88 print ' return new(pool) CYExpression%(infix)s(lhs, rhs);' % locals()
89 print '}'
90 print
91 if postfix != None:
92 print 'CYExpression *CYToken%(name)s::PostfixExpression(apr_pool_t *pool, CYExpression *rhs) const {' % locals()
93 print ' return new(pool) CYExpression%(postfix)s(rhs);' % locals()
94 print '}'
95 print
96 if assign != None:
97 print 'CYExpression *CYToken%(name)s::AssignmentExpression(apr_pool_t *pool, CYExpression *lhs, CYExpression *rhs) const {' % locals()
98 print ' return new(pool) CYExpressionAssign%(assign)s(lhs, rhs);' % locals()
99 print '}'
100 print