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): ...@@ -959,32 +959,7 @@ def fix_expression_types(expr, context):
# the type of the raw PrimSequenceOf can be set now # the type of the raw PrimSequenceOf can be set now
value.exprType.type = asn_type value.exprType.type = asn_type
# Type check that is specific to IN expressions
if isinstance(expr, ogAST.ExprIn): 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 return
if expr.right.is_raw == expr.left.is_raw == False: if expr.right.is_raw == expr.left.is_raw == False:
...@@ -1057,19 +1032,6 @@ def fix_expression_types(expr, context): ...@@ -1057,19 +1032,6 @@ def fix_expression_types(expr, context):
# # the type of the raw PrimSequenceOf can be set now # # the type of the raw PrimSequenceOf can be set now
# expr.right.exprType.type = asn_type # 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: if expr.right.is_raw != expr.left.is_raw:
check_type_compatibility(raw_expr, ref_type, context) check_type_compatibility(raw_expr, ref_type, context)
if not raw_expr.exprType.kind.startswith(('Integer', 'Real')): if not raw_expr.exprType.kind.startswith(('Integer', 'Real')):
...@@ -1442,10 +1404,19 @@ def logic_expression(root, context): ...@@ -1442,10 +1404,19 @@ def logic_expression(root, context):
except (AttributeError, TypeError) as err: except (AttributeError, TypeError) as err:
errors.append(incompatible_types(expr, str(err))) errors.append(incompatible_types(expr, str(err)))
# if both sides are arrays, then the result is an array too left_bty = find_basic_type(expr.left.exprType)
basic_left = find_basic_type(expr.left.exprType) right_bty = find_basic_type(expr.right.exprType)
basic_right = find_basic_type(expr.right.exprType) for bty in left_bty, right_bty:
if basic_left.kind == basic_right.kind == 'BooleanType': 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 expr.exprType = BOOLEAN
else: else:
expr.exprType = expr.left.exprType expr.exprType = expr.left.exprType
...@@ -1514,19 +1485,39 @@ def in_expression(root, context): ...@@ -1514,19 +1485,39 @@ def in_expression(root, context):
root.children[0], root.children[1] = root.children[1], root.children[0] root.children[0], root.children[1] = root.children[1], root.children[0]
expr, errors, warnings = binary_expression(root, context) expr, errors, warnings = binary_expression(root, context)
expr.exprType = BOOLEAN
try: try:
fix_expression_types(expr, context) fix_expression_types(expr, context)
except (AttributeError, TypeError) as err: except (AttributeError, TypeError) as err:
errors.append(incompatible_types(expr, str(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 return expr, errors, warnings
......
...@@ -18,113 +18,113 @@ options { ...@@ -18,113 +18,113 @@ options {
} }
tokens { 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; ACTION;
PROCEDURE_CALL; ALL;
PROCEDURE;
SET;
RESET;
EXPORT;
DECISION;
ALTERNATIVE; ALTERNATIVE;
ANSWER; ANSWER;
ARRAY;
ASN1;
ASSIGN;
BITSTR;
BLOCK;
CHANNEL;
CHOICE;
CIF;
CLOSED_RANGE; CLOSED_RANGE;
OPEN_RANGE; COMMENT;
COMPOSITE_STATE;
CONNECT;
CONNECTION;
CONSTANT; CONSTANT;
CONSTANTS;
DCL;
DECISION;
DIGITS;
ELSE; ELSE;
ALL; EMPTYSTR;
TO; ENDNEWTYPE;
VIA; ENDSYNTYPE;
VIAPATH; 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;
OUTPUT_BODY; OUTPUT_BODY;
ASSIGN; PARAM;
TEXT; PARAMNAMES;
NEXTSTATE;
JOIN;
TERMINATOR;
RETURN;
PROCEDURE_NAME;
PARAMS; PARAMS;
FIELD_NAME;
PRIMARY; PRIMARY;
PRIMARY_ID; PRIMARY_ID;
IF; PROCEDURE;
THEN; PROCEDURE_CALL;
FI; PROCEDURE_NAME;
CIF; PROCESS;
HYPERLINK; PROVIDED;
STATELIST;
INPUTLIST;
INFORMAL_TEXT;
VARIABLE;
VARIABLES;
SORT;
DCL;
NEG;
GROUND;
TEXTAREA;
TEXTAREA_CONTENT;
TASK;
TASK_BODY;
QUESTION; QUESTION;
FPAR; RANGE;
PARAM; RESET;
INOUT; RETURN;
EXTERNAL;
IN;
STOP;
SYSTEM;
USE;
SIGNAL;
CHANNEL;
CONNECTION;
ROUTE; ROUTE;
BLOCK; SAVE;
PARAMNAMES; SEQOF;
ASN1; SEQUENCE;
FLOATING_LABEL; SET;
COMPOSITE_STATE; SIGNAL;
CONNECT; SIGNAL_LIST;
SYNTYPE; SORT;
ENDSYNTYPE; STATE;
NEWTYPE; STATELIST;
ENDNEWTYPE; STIMULUS;
ARRAY; STOP;
CONSTANTS; STRING;
STRUCT; STRUCT;
FIELDS;
FIELD;
SYNONYM; SYNONYM;
SYNONYM_LIST; 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 import sys
from antlr3 import * from antlr3 import *
...@@ -9,30 +9,30 @@ from antlr3.compat import set, frozenset ...@@ -9,30 +9,30 @@ from antlr3.compat import set, frozenset
HIDDEN = BaseRecognizer.HIDDEN HIDDEN = BaseRecognizer.HIDDEN
# token types # token types
NUMBER_OF_INSTANCES=24 NUMBER_OF_INSTANCES=58
COMMENT2=211 COMMENT2=211
MANTISSA=170 MANTISSA=170
ROUTE=93 ROUTE=77
MOD=156 MOD=156
GROUND=76 GROUND=43
PARAM=83 PARAM=63
NOT=158 NOT=158
SEQOF=13 SEQOF=79
TEXTAREA_CONTENT=78 TEXTAREA_CONTENT=100
EOF=-1 EOF=-1
ACTION=33 ACTION=4
IMPORT=174 IMPORT=174
CREATE=145 CREATE=145
FPAR=82 FPAR=42
NEXTSTATE=54 NEXTSTATE=57
RETURN=57 RETURN=76
THIS=146 THIS=146
VIAPATH=49 CHANNEL=13
CHANNEL=91 VIAPATH=110
ENDCONNECTION=123 ENDCONNECTION=123
EXPORT=38 EXPORT=31
EQ=139 EQ=139
INFORMAL_TEXT=70 INFORMAL_TEXT=48
GEODE=178 GEODE=178
D=185 D=185
E=188 E=188
...@@ -47,185 +47,185 @@ L=187 ...@@ -47,185 +47,185 @@ L=187
M=192 M=192
N=183 N=183
O=197 O=197
TERMINATOR=56 TERMINATOR=97
H=198 H=198
I=194 I=194
ELSE=45 ELSE=26
J=205 J=205
K=189 K=189
U=201 U=201
T=199 T=199
W=203 W=203
STOP=87 STOP=88
V=202 V=202
INT=121 INT=121
Q=212 Q=212
P=190 P=190
S=193 S=193
VALUE=10 VALUE=106
R=191 R=191
Y=184 Y=184
X=200 X=200
FI=65 FI=34
Z=213 Z=213
MINUS_INFINITY=166 MINUS_INFINITY=166
WS=210 WS=210
OUT=129 OUT=129
FloatingPointLiteral=167 FloatingPointLiteral=167
NONE=130 NONE=130
INPUT_NONE=27 INPUT_NONE=51
CONSTANT=44 CONSTANT=21
GT=141 GT=141
CALL=135 CALL=135
END=180 END=180
FLOATING_LABEL=97 FLOATING_LABEL=40
T__215=215 T__215=215
IFTHENELSE=8 IFTHENELSE=46
T__216=216 T__216=216
T__214=214 T__214=214
T__219=219 T__219=219
T__217=217 T__217=217
T__218=218 T__218=218
INPUT=31 INPUT=50
ENDSUBSTRUCTURE=128 ENDSUBSTRUCTURE=128
FLOAT=15 FLOAT=39
SUBSTRUCTURE=127 SUBSTRUCTURE=127
ASTERISK=126 ASTERISK=126
INOUT=84
T__220=220 T__220=220
INOUT=49
STR=207 STR=207
STIMULUS=32 STIMULUS=87
THEN=64 THEN=101
ENDDECISION=137 ENDDECISION=137
OPEN_RANGE=43 OPEN_RANGE=60
SIGNAL=90 SIGNAL=82
ENDSYSTEM=111 ENDSYSTEM=111
PLUS=152 PLUS=152
CHOICE=11 CHOICE=14
TASK_BODY=80 TASK_BODY=96
PARAMS=59 PARAMS=65
CLOSED_RANGE=42 CLOSED_RANGE=16
STATE=26 STATE=85
STATELIST=68 STATELIST=86
TO=47 TO=103
ASSIG_OP=181 ASSIG_OP=181
SIGNALROUTE=116 SIGNALROUTE=116
ENDSYNTYPE=101 ENDSYNTYPE=29
SORT=73 SORT=84
SET=36 SET=81
TEXT=53 TEXT=98
SEMI=124 SEMI=124
TEXTAREA=77 TEXTAREA=99
StringLiteral=163 StringLiteral=163
BLOCK=94 BLOCK=12
CIF=66 CIF=15
START=122 START=122
DECISION=39 DECISION=24
DIV=155 DIV=155
PROCESS=23 PROCESS=71
STRING=19 STRING=89
INPUTLIST=69 INPUTLIST=52
EXTERNAL=85 EXTERNAL=33
LT=142 LT=142
EXPONENT=172 EXPONENT=172
TRANSITION=25 TRANSITION=104
ENDBLOCK=115 ENDBLOCK=115
RESET=37 RESET=75
ENDNEWTYPE=103 ENDNEWTYPE=28
BitStringLiteral=159 BitStringLiteral=159
SIGNAL_LIST=30 SIGNAL_LIST=83
ENDTEXT=22 ENDTEXT=30
CONNECTION=92 CONNECTION=20
SYSTEM=88 SYSTEM=94
CONNECT=99 CONNECT=19
L_PAREN=132 L_PAREN=132
PROCEDURE_CALL=34 PROCEDURE_CALL=69
BASE=171 BASE=171
COMMENT=9 COMMENT=17
SYNONYM=109 SYNONYM=91
ENDALTERNATIVE=136 ENDALTERNATIVE=136
ARRAY=104 ARRAY=8
ACTIVE=173 ACTIVE=173
ENDFOR=148 ENDFOR=148
FIELD_NAME=60 FIELD_NAME=36
OCTSTR=18 OCTSTR=59
VIEW=175 VIEW=175
EMPTYSTR=14 EMPTYSTR=27
ENDCHANNEL=112 ENDCHANNEL=112
NULL=164 NULL=164
ANSWER=41 ANSWER=7
PRIMARY=61 PRIMARY=66
TASK=79 TASK=95
REFERENCED=118 REFERENCED=118
ALPHA=208 ALPHA=208
SEQUENCE=12 SEQUENCE=80
VARIABLE=71 VARIABLE=107
PRIORITY=131 PRIORITY=131
SPECIFIC=177 SPECIFIC=177
OR=150 OR=150
COMPOSITE_STATE=98 COMPOSITE_STATE=18
OctetStringLiteral=160 OctetStringLiteral=160
FIELD=108