Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | 29x 29x 29x 29x 326x 326x 326x 326x 326x 326x 326x 29x 1305x 1305x 29x 1218x 1218x 1218x 1218x 1305x 232x 232x 232x 12x 12x 12x 232x 522x 522x 522x 103x 103x 103x 103x 522x 203x 203x 203x 14x 14x 14x 14x 203x 29x 145x 145x 30x 30x 30x 30x 30x 544x 544x 544x 326x 326x 544x 544x 326x 2087x 213x 3x 210x 4x 319x 319x 1761x 1761x 1761x 491x 491x 826x 444x 444x 1761x 1761x 1761x 374x 1761x 1020x 1020x 1020x 344x 344x 344x 1020x | import { SymbolContainer } from './symbolContainer';
import { SymbolTemplate } from './symbolTemplate';
import { ITokens } from '../../interface/exported';
/**
* Using Douglas Crockford paper on Vaughan Pratt "Top Down Operator Precedence"
* For more info about this look here
* https://crockford.com/javascript/tdop/tdop.html (added to docs folder so I dont loose it)
* Not everything is used
*
* This class will do most of the heavy lifting of generating the AST for our expressions
* It will need symbols added and a symbol container
*
*/
export class AST {
public behavior: any;
public valueConverter: any;
private tokens: ITokens[];
public currentStatement: any = null;
public currentTokenIndex = 0;
public currentToken: any;
private symbolContainer: SymbolContainer;
public statementsArray: any[];
/**
* start the parsing of tokens
*
*/
public start(tokens: ITokens[]) {
// set tokens
this.tokens = tokens;
// clear
this.currentStatement = null;
this.currentTokenIndex = 0;
this.currentToken = null;
// get first token
this.advance();
// generate ast
const ast = this.statements();
// return ast
return ast;
}
/**
* adds symbol container we will use to get/set our symbols
*
*/
public addSymbolContainer(symbolContainer: SymbolContainer) {
this.symbolContainer = symbolContainer;
}
/**
* Symbol id and an optional binding power that defaults to 0 and returns a symbol object for that id.
* If the symbol already exists in the symbol_table, the function returns that symbol object.
* Otherwise, it makes a new symbol object that inherits from the symbolTemplate, stores it in the symbol container, and returns it.
* A symbol object initially contains an id, a value, a left binding power, and the stuff it inherits from the symbolTemplate.
*
*/
public symbol(id: string, bp?: number) {
let s = this.symbolContainer[id];
bp = bp || 0;
if (s) {
if (bp >= s.lbp) {
s.lbp = bp;
}
} else {
s = new SymbolTemplate();
s.id = s.value = id;
s.lbp = bp;
this.symbolContainer[id] = s;
}
return s;
}
/**
* Prefix operators are right associative.
* A prefix does not have a left binding power because it does not bind to the left.
* Prefix operators can also sometimes be reserved words.
*
*/
public prefix(id: string, nud?: Function) {
const s = this.symbol(id);
const astInstance = this;
s.nud = nud || function () {
// scope.reserve(this);
this.first = astInstance.expression(70);
this.arity = 'unary';
return this;
};
return s;
}
/**
* The infix function takes an id, a binding power, and an optional led function.
* If a led function is not provided, the infix function supplies a default led that is useful in most cases.
*
*/
public infix(id: string, bp?: number, led?: Function) {
const s = this.symbol(id, bp);
const astInstance = this;
s.led = led || function (left: any) {
this.first = left;
this.second = astInstance.expression(bp);
this.arity = 'binary';
return this;
};
return s;
}
/**
* Those infix operators are left associative.
* We can also make right associative operators, such as short-circuiting logical operators, by reducing the right binding power.
*
*/
public infixr(id: string, bp: number, led?: Function) {
const s = this.symbol(id, bp);
const astInstance = this;
s.led = led || function (left: any) {
this.first = left;
this.second = astInstance.expression(bp - 1);
this.arity = 'binary';
return this;
};
return s;
}
/**
* The stmt function is used to add statement symbols to the symbol table. It takes a statement id and an std function.
*
*/
public stmt(id: string, f: Function) {
const x = this.symbol(id);
x.std = f;
return x;
}
/**
* We could use infixr to define our assignment operators,
* but we will make a specialized assignment function because we want it to do two extra bits of business:
* examine the left operand to make sure that it is a proper lvalue,
* and set an assignment member so that we can later quickly identify assignment statements.
*/
public assignment = function (id: string) {
const astInstance = this;
return this.infixr(id, 10, function (left: any) {
if (left.id !== '.' && left.id !== '[' &&
left.arity !== 'variable') {
left.error('Bad lvalue.');
}
this.first = left;
this.second = astInstance.expression(9);
this.assignment = true;
this.arity = 'binary';
return this;
});
};
/**
* The statement function parses one statement.
* If the current token has an std method, the token is reserved and the std is invoked.
* Otherwise,we assume an expression statement terminated with a semi-colon.
* For reliability, we will reject an expression statement that is not an assignment or invocation.
*
*/
public statement() {
const n = this.currentToken;
let v;
if (n.std) {
this.advance();
// scope.reserve(n);
return n.std();
}
v = this.expression(0);
return v;
}
/**
* The statements function parses statements until it sees (end) or } which signals the end of a block.
* The function returns a statement, an array of statements, or null if there were no statements present.
*
*/
public statements() {
this.statementsArray = [];
let s;
while (true) {
if (this.currentToken.id === '}' || this.currentToken.id === '(end)') {
break;
}
s = this.statement();
if (s) {
this.statementsArray.push(s);
}
}
return this.statementsArray.length === 0 ? null : this.statementsArray.length === 1 ? this.statementsArray[0] : this.statementsArray;
}
/**
* The advance function makes a new token object from the next simple token in the array and assigns it to the token variable.
* It can take an optional id parameter which it can check against the id of the previous token.
* The new token object's prototype is a (name) token in the current scope or a symbol from the symbol table.
* The new token's arity is "name", "literal", or "operator".
* Its arity may be changed later to "binary", "unary", or "statement" when we know more about the token's role in the program.
*
*/
public advance(expected?: string) {
let type, o, token, value;
token = this.tokens[this.currentTokenIndex];
if (token && expected) {
const nextToken = this.tokens[this.currentTokenIndex + 1];
if (expected === '}' && nextToken && (nextToken.value === '|' || nextToken.value === '&')) {
return;
}
// -> will happend if signle inside expression ${values | valuconverter}
const prevToken = this.tokens[this.currentTokenIndex - 1];
if (expected === '}' && prevToken && (prevToken.value === '|' || prevToken.value === '&')) {
return;
}
}
if (this.currentTokenIndex >= this.tokens.length) {
this.currentToken = this.symbolContainer['(end)'];
return;
}
this.currentTokenIndex += 1;
value = token.value;
type = token.type;
if (type === 'variable') {
type = 'variable';
o = this.symbolContainer['(variable)'];
} else if (type === 'operator') {
o = this.symbolContainer[value];
if (!o) {
console.warn('Unknown operator.', token);
}
} else if (type === 'string' || type === 'number') {
type = 'literal';
o = this.symbolContainer['(literal)'];
} else {
console.warn('Unexpected token.', token);
}
this.currentToken = Object.create(o);
this.currentToken.value = value;
this.currentToken.arity = type;
if ((<any>token).root) {
this.currentToken.root = true;
}
return this.currentToken;
}
/**
* The heart of Pratt's technique is the expression function.
* It takes a right binding power that controls how aggressively it binds to tokens on its right.
*
*/
public expression(rbp: any) {
let left;
let token = this.currentToken;
this.advance();
left = token.nud();
try {
while (rbp < this.currentToken.lbp) {
token = this.currentToken;
this.advance();
left = token.led(left);
}
} catch (e) {
console.warn('parser fail');
}
return left;
}
}
|