Commit 75df763f authored by dbarbera's avatar dbarbera
Browse files

Sort tokens alphabetically

parent e9947988
......@@ -959,32 +959,7 @@ def fix_expression_types(expr, context):
# the type of the raw PrimSequenceOf can be set now
value.exprType.type = asn_type
# Type check that is specific to IN expressions
if isinstance(expr, ogAST.ExprIn):
# check that left part is a SEQUENCE OF or a string
container_basic_type = find_basic_type(expr.left.exprType)
if container_basic_type.kind == 'SequenceOfType':
ref_type = container_basic_type.type
elif container_basic_type.kind.endswith('StringType'):
ref_type = container_basic_type
else:
raise TypeError('IN expression: right part must be a list')
compare_types(expr.right.exprType, ref_type)
if expr.right.is_raw == expr.left.is_raw == True:
# If both sides are raw (e.g. "3 in {1,2,3}"), evaluate expression
bool_expr = ogAST.PrimBoolean()
bool_expr.inputString = expr.inputString
bool_expr.line = expr.line
bool_expr.charPositionInLine = expr.charPositionInLine
bool_expr.exprType = type('PrBool', (object,),
{'kind': 'BooleanType'})
if expr.right.value in [each.value for each in expr.left.value]:
bool_expr.value = ['true']
raise Warning('Expression is always true', bool_expr)
else:
bool_expr.value = ['false']
raise Warning('Expression is always false', bool_expr)
return
if expr.right.is_raw == expr.left.is_raw == False:
......@@ -1057,19 +1032,6 @@ def fix_expression_types(expr, context):
# # the type of the raw PrimSequenceOf can be set now
# expr.right.exprType.type = asn_type
if isinstance(expr, (ogAST.ExprAnd, ogAST.ExprOr, ogAST.ExprXor)):
# Bitwise operators: check that both sides are booleans
for side in expr.left, expr.right:
basic_type = find_basic_type(side.exprType)
if basic_type.kind in ('BooleanType', 'BitStringType'):
continue
elif basic_type.kind == 'SequenceOfType':
if find_basic_type(side.exprType).type.kind == 'BooleanType':
continue
else:
raise TypeError('Bitwise operators only work with '
'booleans and arrays of booleans')
if expr.right.is_raw != expr.left.is_raw:
check_type_compatibility(raw_expr, ref_type, context)
if not raw_expr.exprType.kind.startswith(('Integer', 'Real')):
......@@ -1442,10 +1404,19 @@ def logic_expression(root, context):
except (AttributeError, TypeError) as err:
errors.append(incompatible_types(expr, str(err)))
# if both sides are arrays, then the result is an array too
basic_left = find_basic_type(expr.left.exprType)
basic_right = find_basic_type(expr.right.exprType)
if basic_left.kind == basic_right.kind == 'BooleanType':
left_bty = find_basic_type(expr.left.exprType)
right_bty = find_basic_type(expr.right.exprType)
for bty in left_bty, right_bty:
if bty.kind in ('BooleanType', 'BitStringType'):
continue
elif bty.kind == 'SequenceOfType' and bty.type.kind == 'BooleanType':
continue
else:
msg = 'Bitwise operators only work with booleans and arrays of booleans'
errors.append(incompatible_types(expr, msg))
# TODO: Is this correct?
if left_bty.kind == right_bty.kind == 'BooleanType':
expr.exprType = BOOLEAN
else:
expr.exprType = expr.left.exprType
......@@ -1514,19 +1485,39 @@ def in_expression(root, context):
root.children[0], root.children[1] = root.children[1], root.children[0]
expr, errors, warnings = binary_expression(root, context)
expr.exprType = BOOLEAN
try:
fix_expression_types(expr, context)
except (AttributeError, TypeError) as err:
errors.append(incompatible_types(expr, str(err)))
except Warning as warn:
# warnings are raised when an expression returns always true or
# false. In that case a new expression is returned
report, new_expr = warn.args
warnings.append('Expression "%s" : %s' % (expr.inputString, str(report)))
expr = new_expr
expr.exprType = BOOLEAN
# check that left part is a SEQUENCE OF or a string
container_basic_type = find_basic_type(expr.left.exprType)
if container_basic_type.kind == 'SequenceOfType':
ref_type = container_basic_type.type
elif container_basic_type.kind.endswith('StringType'):
ref_type = container_basic_type
else:
msg = 'IN expression: right part must be a list'
errors.append(incompatible_types(expr, msg))
return expr, errors, warnings
compare_types(expr.right.exprType, ref_type)
if expr.right.is_raw and expr.left.is_raw:
# If both sides are raw (e.g. "3 in {1,2,3}"), evaluate expression
bool_expr = ogAST.PrimBoolean()
bool_expr.inputString = expr.inputString
bool_expr.line = expr.line
bool_expr.charPositionInLine = expr.charPositionInLine
bool_expr.exprType = type('PrBool', (object,), {'kind': 'BooleanType'})
if expr.right.value in [each.value for each in expr.left.value]:
bool_expr.value = ['true']
warnings.append('Expression %s is always true' % expr.inputString)
else:
bool_expr.value = ['false']
warnings.append('Expression %s is always false' % expr.inputString)
expr = bool_expr
return expr, errors, warnings
......
......@@ -18,113 +18,113 @@ options {
}
tokens {
FOR;
RANGE;
TIMER;
LABEL;
IFTHENELSE;
COMMENT;
VALUE;
CHOICE;
SEQUENCE;
SEQOF;
EMPTYSTR;
FLOAT;
FLOAT2;
BITSTR;
OCTSTR;
STRING;
EXPRESSION;
DIGITS;
ENDTEXT;
PROCESS;
NUMBER_OF_INSTANCES;
TRANSITION;
STATE;
INPUT_NONE;
SAVE;
PROVIDED;
SIGNAL_LIST;
INPUT;
STIMULUS;
ACTION;
PROCEDURE_CALL;
PROCEDURE;
SET;
RESET;
EXPORT;
DECISION;
ALL;
ALTERNATIVE;
ANSWER;
ARRAY;
ASN1;
ASSIGN;
BITSTR;
BLOCK;
CHANNEL;
CHOICE;
CIF;
CLOSED_RANGE;
OPEN_RANGE;
COMMENT;
COMPOSITE_STATE;
CONNECT;
CONNECTION;
CONSTANT;
CONSTANTS;
DCL;
DECISION;
DIGITS;
ELSE;
ALL;
TO;
VIA;
VIAPATH;
EMPTYSTR;
ENDNEWTYPE;
ENDSYNTYPE;
ENDTEXT;
EXPORT;
EXPRESSION;
EXTERNAL;
FI;
FIELD;
FIELD_NAME;
FIELDS;
FLOAT2;
FLOAT;
FLOATING_LABEL;
FOR;
FPAR;
GROUND;
HYPERLINK;
IF;
IFTHENELSE;
IN;
INFORMAL_TEXT;
INOUT;
INPUT;
INPUT_NONE;
INPUTLIST;
JOIN;
LABEL;
NEG;
NEWTYPE;
NEXTSTATE;
NUMBER_OF_INSTANCES;
OCTSTR;
OPEN_RANGE;
OUTPUT;
OUTPUT_BODY;
ASSIGN;
TEXT;
NEXTSTATE;
JOIN;
TERMINATOR;
RETURN;
PROCEDURE_NAME;
PARAM;
PARAMNAMES;
PARAMS;
FIELD_NAME;
PRIMARY;
PRIMARY_ID;
IF;
THEN;
FI;
CIF;
HYPERLINK;
STATELIST;
INPUTLIST;
INFORMAL_TEXT;
VARIABLE;
VARIABLES;
SORT;
DCL;
NEG;
GROUND;
TEXTAREA;
TEXTAREA_CONTENT;
TASK;
TASK_BODY;
PROCEDURE;
PROCEDURE_CALL;
PROCEDURE_NAME;
PROCESS;
PROVIDED;
QUESTION;
FPAR;
PARAM;
INOUT;
EXTERNAL;
IN;
STOP;
SYSTEM;
USE;
SIGNAL;
CHANNEL;
CONNECTION;
RANGE;
RESET;
RETURN;
ROUTE;
BLOCK;
PARAMNAMES;
ASN1;
FLOATING_LABEL;
COMPOSITE_STATE;
CONNECT;
SYNTYPE;
ENDSYNTYPE;
NEWTYPE;
ENDNEWTYPE;
ARRAY;
CONSTANTS;
SAVE;
SEQOF;
SEQUENCE;
SET;
SIGNAL;
SIGNAL_LIST;
SORT;
STATE;
STATELIST;
STIMULUS;
STOP;
STRING;
STRUCT;
FIELDS;
FIELD;
SYNONYM;
SYNONYM_LIST;
SYNTYPE;
SYSTEM;
TASK;
TASK_BODY;
TERMINATOR;
TEXT;
TEXTAREA;
TEXTAREA_CONTENT;
THEN;
TIMER;
TO;
TRANSITION;
USE;
VALUE;
VARIABLE;
VARIABLES;
VIA;
VIAPATH;
}
......
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-07-07 18:45:53
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-07-09 01:19:26
import sys
from antlr3 import *
......@@ -9,30 +9,30 @@ from antlr3.compat import set, frozenset
HIDDEN = BaseRecognizer.HIDDEN
# token types
NUMBER_OF_INSTANCES=24
NUMBER_OF_INSTANCES=58
COMMENT2=211
MANTISSA=170
ROUTE=93
ROUTE=77
MOD=156
GROUND=76
PARAM=83
GROUND=43
PARAM=63
NOT=158
SEQOF=13
TEXTAREA_CONTENT=78
SEQOF=79
TEXTAREA_CONTENT=100
EOF=-1
ACTION=33
ACTION=4
IMPORT=174
CREATE=145
FPAR=82
NEXTSTATE=54
RETURN=57
FPAR=42
NEXTSTATE=57
RETURN=76
THIS=146
VIAPATH=49
CHANNEL=91
CHANNEL=13
VIAPATH=110
ENDCONNECTION=123
EXPORT=38
EXPORT=31
EQ=139
INFORMAL_TEXT=70
INFORMAL_TEXT=48
GEODE=178
D=185
E=188
......@@ -47,185 +47,185 @@ L=187
M=192
N=183
O=197
TERMINATOR=56
TERMINATOR=97
H=198
I=194
ELSE=45
ELSE=26
J=205
K=189
U=201
T=199
W=203
STOP=87
STOP=88
V=202
INT=121
Q=212
P=190
S=193
VALUE=10
VALUE=106
R=191
Y=184
X=200
FI=65
FI=34
Z=213
MINUS_INFINITY=166
WS=210
OUT=129
FloatingPointLiteral=167
NONE=130
INPUT_NONE=27
CONSTANT=44
INPUT_NONE=51
CONSTANT=21
GT=141
CALL=135
END=180
FLOATING_LABEL=97
FLOATING_LABEL=40
T__215=215
IFTHENELSE=8
IFTHENELSE=46
T__216=216
T__214=214
T__219=219
T__217=217
T__218=218
INPUT=31
INPUT=50
ENDSUBSTRUCTURE=128
FLOAT=15
FLOAT=39
SUBSTRUCTURE=127
ASTERISK=126
INOUT=84
T__220=220
INOUT=49
STR=207
STIMULUS=32
THEN=64
STIMULUS=87
THEN=101
ENDDECISION=137
OPEN_RANGE=43
SIGNAL=90
OPEN_RANGE=60
SIGNAL=82
ENDSYSTEM=111
PLUS=152
CHOICE=11
TASK_BODY=80
PARAMS=59
CLOSED_RANGE=42
STATE=26
STATELIST=68
TO=47
CHOICE=14
TASK_BODY=96
PARAMS=65
CLOSED_RANGE=16
STATE=85
STATELIST=86
TO=103
ASSIG_OP=181
SIGNALROUTE=116
ENDSYNTYPE=101
SORT=73
SET=36
TEXT=53
ENDSYNTYPE=29
SORT=84
SET=81
TEXT=98
SEMI=124
TEXTAREA=77
TEXTAREA=99
StringLiteral=163
BLOCK=94
CIF=66
BLOCK=12
CIF=15
START=122
DECISION=39
DECISION=24
DIV=155
PROCESS=23
STRING=19
INPUTLIST=69
EXTERNAL=85
PROCESS=71
STRING=89
INPUTLIST=52
EXTERNAL=33
LT=142
EXPONENT=172
TRANSITION=25
TRANSITION=104
ENDBLOCK=115
RESET=37
ENDNEWTYPE=103
RESET=75
ENDNEWTYPE=28
BitStringLiteral=159
SIGNAL_LIST=30
ENDTEXT=22
CONNECTION=92
SYSTEM=88
CONNECT=99
SIGNAL_LIST=83
ENDTEXT=30
CONNECTION=20
SYSTEM=94
CONNECT=19
L_PAREN=132
PROCEDURE_CALL=34
PROCEDURE_CALL=69
BASE=171
COMMENT=9
SYNONYM=109
COMMENT=17
SYNONYM=91
ENDALTERNATIVE=136
ARRAY=104
ARRAY=8
ACTIVE=173
ENDFOR=148
FIELD_NAME=60
OCTSTR=18
FIELD_NAME=36
OCTSTR=59
VIEW=175
EMPTYSTR=14
EMPTYSTR=27
ENDCHANNEL=112
NULL=164
ANSWER=41
PRIMARY=61
TASK=79
ANSWER=7
PRIMARY=66
TASK=95
REFERENCED=118
ALPHA=208
SEQUENCE=12
VARIABLE=71
SEQUENCE=80
VARIABLE=107
PRIORITY=131
SPECIFIC=177
OR=150
COMPOSITE_STATE=98
COMPOSITE_STATE=18
OctetStringLiteral=160
FIELD=108
USE=89
FIELD=35
USE=105
FROM=113
ENDPROCEDURE=120
FALSE=162
OUTPUT=50
SYNONYM_LIST=110
OUTPUT=61
SYNONYM_LIST=92
APPEND=154
L_BRACKET=168
PRIMARY_ID=62
DIGITS=21
HYPERLINK=67
NEWTYPE=102
PRIMARY_ID=67
DIGITS=25
HYPERLINK=44
NEWTYPE=56
Exponent=209
FOR=4
FOR=41
ENDSTATE=125
PROCEDURE_NAME=58
CONSTANTS=105
PROCEDURE_NAME=70
CONSTANTS=22
AND=117
ID=147
FLOAT2=16
IF=63
IN=86
PROVIDED=29
FLOAT2=38
IF=45
IN=47
PROVIDED=72
COMMA=134
ALL=46
ALL=5
ASNFILENAME=179
DOT=206
EXPRESSION=20
EXPRESSION=32
WITH=114
BITSTR=17
BITSTR=11
XOR=151
DASH=153
ENDPROCESS=119
DCL=74
RANGE=5
VIA=48
STRUCT=106
SAVE=28
FIELDS=107
DCL=23
VIA=109
RANGE=74
STRUCT=90
SAVE=78
FIELDS=37
REM=157
TRUE=161
R_BRACKET=169
PROCEDURE=35
JOIN=55
JOIN=53
PROCEDURE=68
R_PAREN=133
OUTPUT_BODY=51
OUTPUT_BODY=62
NEQ=140
ANY=138
QUESTION=81
LABEL=7
QUESTION=73
LABEL=54
PLUS_INFINITY=165
PARAMNAMES=95
ASN1=96
PARAMNAMES=64
ASN1=9
KEEP=176
NEG=75
ASSIGN=52