File size: 9,254 Bytes
18a519f | 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | grammar NCalc;
options
{
output=AST;
ASTLabelType=CommonTree;
language=CSharp;
}
@header {
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using NCalc.Domain;
}
@members {
private const char BS = '\\';
private static NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
private string extractString(string text) {
StringBuilder sb = new StringBuilder(text);
int startIndex = 1; // Skip initial quote
int slashIndex = -1;
while ((slashIndex = sb.ToString().IndexOf(BS, startIndex)) != -1)
{
char escapeType = sb[slashIndex + 1];
switch (escapeType)
{
case 'u':
string hcode = String.Concat(sb[slashIndex+4], sb[slashIndex+5]);
string lcode = String.Concat(sb[slashIndex+2], sb[slashIndex+3]);
char unicodeChar = Encoding.Unicode.GetChars(new byte[] { System.Convert.ToByte(hcode, 16), System.Convert.ToByte(lcode, 16)} )[0];
sb.Remove(slashIndex, 6).Insert(slashIndex, unicodeChar);
break;
case 'n': sb.Remove(slashIndex, 2).Insert(slashIndex, '\n'); break;
case 'r': sb.Remove(slashIndex, 2).Insert(slashIndex, '\r'); break;
case 't': sb.Remove(slashIndex, 2).Insert(slashIndex, '\t'); break;
case '\'': sb.Remove(slashIndex, 2).Insert(slashIndex, '\''); break;
case '\\': sb.Remove(slashIndex, 2).Insert(slashIndex, '\\'); break;
default: throw new RecognitionException("Unvalid escape sequence: \\" + escapeType);
}
startIndex = slashIndex + 1;
}
sb.Remove(0, 1);
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
public List<string> Errors { get; private set; }
public override void DisplayRecognitionError(String[] tokenNames, RecognitionException e) {
base.DisplayRecognitionError(tokenNames, e);
if(Errors == null)
{
Errors = new List<string>();
}
String hdr = GetErrorHeader(e);
String msg = GetErrorMessage(e, tokenNames);
Errors.Add(msg + " at " + hdr);
}
}
@init {
numberFormatInfo.NumberDecimalSeparator = ".";
}
ncalcExpression returns [LogicalExpression value]
: logicalExpression EOF! {$value = $logicalExpression.value; }
;
logicalExpression returns [LogicalExpression value]
: left=conditionalExpression { $value = $left.value; } ( '?' middle=conditionalExpression ':' right=conditionalExpression { $value = new TernaryExpression($left.value, $middle.value, $right.value); })?
;
conditionalExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=booleanAndExpression { $value = $left.value; } (
('||' | 'or') { type = BinaryExpressionType.Or; }
right=conditionalExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
booleanAndExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=bitwiseOrExpression { $value = $left.value; } (
('&&' | 'and') { type = BinaryExpressionType.And; }
right=bitwiseOrExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
bitwiseOrExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=bitwiseXOrExpression { $value = $left.value; } (
'|' { type = BinaryExpressionType.BitwiseOr; }
right=bitwiseOrExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
bitwiseXOrExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=bitwiseAndExpression { $value = $left.value; } (
'^' { type = BinaryExpressionType.BitwiseXOr; }
right=bitwiseAndExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
bitwiseAndExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=equalityExpression { $value = $left.value; } (
'&' { type = BinaryExpressionType.BitwiseAnd; }
right=equalityExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
equalityExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=relationalExpression { $value = $left.value; } (
( ('==' | '=' ) { type = BinaryExpressionType.Equal; }
| ('!=' | '<>' ) { type = BinaryExpressionType.NotEqual; } )
right=relationalExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
relationalExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=shiftExpression { $value = $left.value; } (
( '<' { type = BinaryExpressionType.Lesser; }
| '<=' { type = BinaryExpressionType.LesserOrEqual; }
| '>' { type = BinaryExpressionType.Greater; }
| '>=' { type = BinaryExpressionType.GreaterOrEqual; } )
right=shiftExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
shiftExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=additiveExpression { $value = $left.value; } (
( '<<' { type = BinaryExpressionType.LeftShift; }
| '>>' { type = BinaryExpressionType.RightShift; } )
right=additiveExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
additiveExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=multiplicativeExpression { $value = $left.value; } (
( '+' { type = BinaryExpressionType.Plus; }
| '-' { type = BinaryExpressionType.Minus; } )
right=multiplicativeExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
multiplicativeExpression returns [LogicalExpression value]
@init {
BinaryExpressionType type = BinaryExpressionType.Unknown;
}
: left=unaryExpression { $value = $left.value); } (
( '*' { type = BinaryExpressionType.Times; }
| '/' { type = BinaryExpressionType.Div; }
| '%' { type = BinaryExpressionType.Modulo; } )
right=unaryExpression { $value = new BinaryExpression(type, $value, $right.value); }
)*
;
unaryExpression returns [LogicalExpression value]
: primaryExpression { $value = $primaryExpression.value; }
| ('!' | 'not') primaryExpression { $value = new UnaryExpression(UnaryExpressionType.Not, $primaryExpression.value); }
| ('~') primaryExpression { $value = new UnaryExpression(UnaryExpressionType.BitwiseNot, $primaryExpression.value); }
| '-' primaryExpression { $value = new UnaryExpression(UnaryExpressionType.Negate, $primaryExpression.value); }
;
primaryExpression returns [LogicalExpression value]
: '(' logicalExpression ')' { $value = $logicalExpression.value; }
| expr=value { $value = $expr.value; }
| identifier {$value = (LogicalExpression) $identifier.value; } (arguments {$value = new Function($identifier.value, ($arguments.value).ToArray()); })?
;
value returns [ValueExpression value]
: INTEGER { try { $value = new ValueExpression(int.Parse($INTEGER.text)); } catch(System.OverflowException) { $value = new ValueExpression(long.Parse($INTEGER.text)); } }
| FLOAT { $value = new ValueExpression(double.Parse($FLOAT.text, NumberStyles.Float, numberFormatInfo)); }
| STRING { $value = new ValueExpression(extractString($STRING.text)); }
| DATETIME { $value = new ValueExpression(DateTime.Parse($DATETIME.text.Substring(1, $DATETIME.text.Length-2))); }
| TRUE { $value = new ValueExpression(true); }
| FALSE { $value = new ValueExpression(false); }
;
identifier returns[Identifier value]
: ID { $value = new Identifier($ID.text); }
| NAME { $value = new Identifier($NAME.text.Substring(1, $NAME.text.Length-2)); }
;
expressionList returns [List<LogicalExpression> value]
@init {
List<LogicalExpression> expressions = new List<LogicalExpression>();
}
: first=logicalExpression {expressions.Add($first.value);} ( ',' follow=logicalExpression {expressions.Add($follow.value);})*
{ $value = expressions; }
;
arguments returns [List<LogicalExpression> value]
@init {
$value = new List<LogicalExpression>();
}
: '(' ( expressionList {$value = $expressionList.value;} )? ')'
;
TRUE
: 'true'
;
FALSE
: 'false'
;
ID
: LETTER (LETTER | DIGIT)*
;
INTEGER
: DIGIT+
;
FLOAT
: DIGIT* '.' DIGIT+ E?
| DIGIT+ E
;
STRING
: '\'' ( EscapeSequence | (options {greedy=false;} : ~('\u0000'..'\u001f' | '\\' | '\'' ) ) )* '\''
;
DATETIME
: '#' (options {greedy=false;} : ~('#')*) '#'
;
NAME : '[' (options {greedy=false;} : ~(']')*) ']'
;
E : ('E'|'e') ('+'|'-')? DIGIT+
;
fragment LETTER
: 'a'..'z'
| 'A'..'Z'
| '_'
;
fragment DIGIT
: '0'..'9'
;
fragment EscapeSequence
: '\\'
(
'n'
| 'r'
| 't'
| '\''
| '\\'
| UnicodeEscape
)
;
fragment HexDigit
: ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment UnicodeEscape
: 'u' HexDigit HexDigit HexDigit HexDigit
;
/* Ignore white spaces */
WS : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
;
|