forked from rljacobson/FoxySheep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexermembers.py
85 lines (75 loc) · 2.63 KB
/
lexermembers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# TARGET LANGUAGE DEPENDENT CODE.
# Binary plus follows a complete expression. Complete
# expressions always end with one of the following
# tokens. On the other hand, unary plus never follows
# these tokens. Distinguishing unary plus from binary
# plus disambiguates the grammar and allows us to use
# implicit multiplication.
closeExprTokens = [
FoxySheepParser.NumberLiteral,
FoxySheepParser.Name,
FoxySheepParser.StringLiteral,
FoxySheepParser.RPAREN,
FoxySheepParser.RBRACE,
FoxySheepParser.HASH,
FoxySheepParser.PERCENT,
FoxySheepParser.TRIPPLEBLANK,
FoxySheepParser.DOUBLEBLANK,
FoxySheepParser.BLANK,
FoxySheepParser.HASH,
FoxySheepParser.DOUBLEHASH,
FoxySheepParser.DIGITS,
FoxySheepParser.RBRACKET,
FoxySheepParser.RBARBRACKET,
FoxySheepParser.BoxRightBoxParenthesis,
FoxySheepParser.DOUBLEPLUS,
FoxySheepParser.DOUBLEMINUS,
FoxySheepParser.BANG,
FoxySheepParser.DOUBLEBANG,
FoxySheepParser.CONJUGATE,
FoxySheepParser.TRANSPOSE,
FoxySheepParser.CONJUGATETRANSPOSE,
FoxySheepParser.HERMITIANCONJUGATE,
FoxySheepParser.SINGLEQUOTE,
FoxySheepParser.DOUBLESEMICOLON,
FoxySheepParser.DOUBLEDOT,
FoxySheepParser.TRIPPLEDOT,
FoxySheepParser.AMP,
FoxySheepParser.DOT,
FoxySheepParser.SEMICOLON
]
# To determine if a newline separates expressions, we keep
# track of the bracketing level. Note that we treat all
# bracket-like characters as the same.
bracketLevel = 0
# Curiously, the lexer does not allow us to inspect previous
# tokens. Thus we need to keep track of the previous token
# so that we can use it to disambiguate unary/binary plus.
lastToken = None
def getToken():
lt = super.getToken()
if lt.getChannel() != HIDDEN:
lastToken = lt
return lt
def nextToken():
lt = super.nextToken()
if lt.getChannel() != HIDDEN:
lastToken = lt
return lt
# The following checks to see if the previous token likely
# ended an expr. If so, it returns true. We use this method
# in an action on plus to disambiguate between unary plus
# and binary plus.
def precededByExpr():
#Returns true if the previous token ended a complete expr.
if lastToken is None:
return False
tokenType = lastToken.getType()
return tokenType in closeExprTokens
# The following checks to see if the current token (a newline)
# separates two expressions using the following heuristic:
# If the token follows a complete expression and all bracket-
# like characters have been matched, then the token is an
# expression separator, and we return true.
def expressionSeparator():
return precededByExpr() and bracketLevel == 0