Parser.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace NTERA.Engine.Compiler
  7. {
  8. public class Parser
  9. {
  10. protected Lexer Lexer { get; }
  11. protected FunctionDefinition SelfDefinition { get; }
  12. protected ICollection<FunctionDefinition> FunctionDefinitions { get; }
  13. protected ICollection<FunctionDefinition> ProcedureDefinitions { get; }
  14. protected ICollection<FunctionVariable> ConstantDefinitions { get; }
  15. protected ICollection<FunctionVariable> GlobalVariables { get; }
  16. protected ICollection<FunctionVariable> LocalVariables { get; }
  17. protected ICollection<Keyword> ExplicitKeywords { get; }
  18. protected CSVDefinition CsvDefinition { get; }
  19. protected List<ParserError> Errors { get; } = new List<ParserError>();
  20. protected List<ParserError> Warnings { get; } = new List<ParserError>();
  21. protected IEnumerator<Token> Enumerator { get; }
  22. protected bool hasPeeked = false;
  23. protected Token peekedToken = Token.Unknown;
  24. protected Token GetNextToken(bool peek = false)
  25. {
  26. if (peek && hasPeeked)
  27. return peekedToken;
  28. if (!hasPeeked)
  29. Enumerator.MoveNext();
  30. peekedToken = Enumerator.Current;
  31. hasPeeked = peek;
  32. return Enumerator.Current;
  33. }
  34. protected Marker CurrentPosition => new Marker(Lexer.TokenMarker.Pointer + SelfDefinition.Position.Pointer,
  35. Lexer.TokenMarker.Line + SelfDefinition.Position.Line - 1,
  36. Lexer.TokenMarker.Column);
  37. public Parser(string input, FunctionDefinition selfDefinition, ICollection<FunctionDefinition> functionDefinitions, ICollection<FunctionDefinition> procedureDefinitions, ICollection<FunctionVariable> globalVariables, ICollection<FunctionVariable> localVariables, ICollection<Keyword> explicitKeywords, CSVDefinition csvDefinition, ICollection<FunctionVariable> constantDefinitions)
  38. {
  39. Lexer = new Lexer(input);
  40. Enumerator = Lexer.GetEnumerator();
  41. SelfDefinition = selfDefinition;
  42. FunctionDefinitions = functionDefinitions;
  43. ProcedureDefinitions = procedureDefinitions;
  44. ConstantDefinitions = constantDefinitions;
  45. GlobalVariables = globalVariables;
  46. LocalVariables = localVariables;
  47. ExplicitKeywords = explicitKeywords;
  48. CsvDefinition = csvDefinition;
  49. }
  50. public IEnumerable<ExecutionNode> Parse(out List<ParserError> errors, out List<ParserError> warnings)
  51. {
  52. List<ExecutionNode> nodes = new List<ExecutionNode>();
  53. using (Enumerator)
  54. {
  55. do
  56. {
  57. var node = ParseLine(out var error);
  58. if (error != null)
  59. {
  60. Errors.Add(error);
  61. nodes.Add(new ExecutionNode
  62. {
  63. Type = "error",
  64. Metadata =
  65. {
  66. ["message"] = error.ErrorMessage,
  67. ["symbol"] = error.SymbolMarker.ToString()
  68. },
  69. Symbol = error.SymbolMarker
  70. });
  71. //resynchronize to a new line
  72. while (Enumerator.MoveNext()
  73. && Enumerator.Current != Token.NewLine
  74. && Enumerator.Current != Token.EOF)
  75. {
  76. }
  77. }
  78. else if (node != null)
  79. {
  80. nodes.Add(node);
  81. }
  82. hasPeeked = false;
  83. } while (Enumerator.MoveNext());
  84. }
  85. errors = Errors;
  86. warnings = Warnings;
  87. if (errors.Count == 0)
  88. PostProcess(nodes);
  89. return nodes;
  90. }
  91. #region Processor
  92. protected ExecutionNode ParseLine(out ParserError error)
  93. {
  94. error = null;
  95. switch (Enumerator.Current)
  96. {
  97. case Token.Identifer:
  98. if (IsVariable(Lexer.Identifier))
  99. {
  100. string variableName = Lexer.Identifier;
  101. ValueType type = 0;
  102. if (GlobalVariables.Any(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)))
  103. type = GlobalVariables.First(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)).ValueType;
  104. else if (LocalVariables.Any(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)))
  105. type = LocalVariables.First(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)).ValueType;
  106. else if (ConstantDefinitions.Any(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)))
  107. type = ConstantDefinitions.First(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)).ValueType;
  108. var node = new ExecutionNode
  109. {
  110. Type = "assignment",
  111. Symbol = CurrentPosition
  112. };
  113. var variable = GetVariable(out error);
  114. if (error != null)
  115. return null;
  116. if (GetNextToken() != Token.Equal
  117. && Enumerator.Current != Token.Increment
  118. && Enumerator.Current != Token.Decrement
  119. && Enumerator.Current != Token.Append
  120. && !Enumerator.Current.IsArithmetic())
  121. {
  122. error = new ParserError($"Unexpected token, expecting assignment: {Enumerator.Current}", CurrentPosition);
  123. return null;
  124. }
  125. ExecutionNode value;
  126. if (Enumerator.Current == Token.Increment)
  127. {
  128. value = OperateNodes(variable, CreateConstant(1, CurrentPosition), Token.Plus);
  129. }
  130. else if (Enumerator.Current == Token.Decrement)
  131. {
  132. value = OperateNodes(variable, CreateConstant(1, CurrentPosition), Token.Minus);
  133. }
  134. else if (Enumerator.Current == Token.Append)
  135. {
  136. value = OperateNodes(variable, Expression(out error), Token.Plus);
  137. if (error != null)
  138. return null;
  139. }
  140. else if (Enumerator.Current != Token.Equal)
  141. {
  142. Token arithmeticToken = Enumerator.Current;
  143. if (GetNextToken() != Token.Equal)
  144. {
  145. error = new ParserError($"Unexpected token, expecting assignment: {Enumerator.Current}", CurrentPosition);
  146. return null;
  147. }
  148. ExecutionNode newValue = Expression(out error);
  149. value = OperateNodes(variable, newValue, arithmeticToken);
  150. }
  151. else
  152. {
  153. value = type == ValueType.String
  154. ? ParseString(out error, true, true)
  155. : Expression(out error);
  156. }
  157. if (error != null)
  158. return null;
  159. node.SubNodes = new List<ExecutionNode>
  160. {
  161. variable,
  162. new ExecutionNode
  163. {
  164. Type = "value",
  165. SubNodes = new List<ExecutionNode> { value }
  166. }
  167. };
  168. return node;
  169. }
  170. else if (Lexer.Identifier.Equals("CASE", StringComparison.OrdinalIgnoreCase))
  171. {
  172. var node = new ExecutionNode
  173. {
  174. Type = "case",
  175. Symbol = CurrentPosition,
  176. SubNodes = new List<ExecutionNode>()
  177. };
  178. do
  179. {
  180. if (GetNextToken(true) == Token.NewLine
  181. || GetNextToken(true) == Token.EOF)
  182. break;
  183. var value = Expression(out error);
  184. if (error != null)
  185. return null;
  186. if (Enumerator.Current == Token.To)
  187. {
  188. var value2 = Expression(out error);
  189. if (error != null)
  190. return null;
  191. node.SubNodes.Add(new ExecutionNode
  192. {
  193. Type = "case-to",
  194. SubNodes = new List<ExecutionNode> { value, value2 }
  195. });
  196. continue;
  197. }
  198. node.SubNodes.Add(new ExecutionNode
  199. {
  200. Type = "case-exact",
  201. SubNodes = new List<ExecutionNode> { value }
  202. });
  203. } while (Enumerator.Current == Token.Comma);
  204. if (Enumerator.Current != Token.NewLine
  205. && Enumerator.Current != Token.EOF)
  206. {
  207. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  208. return null;
  209. }
  210. return node;
  211. }
  212. else if (Lexer.Identifier.Equals("CALL", StringComparison.OrdinalIgnoreCase)
  213. || Lexer.Identifier.Equals("TRYCALL", StringComparison.OrdinalIgnoreCase))
  214. {
  215. Enumerator.MoveNext();
  216. if (Enumerator.Current != Token.Identifer)
  217. {
  218. error = new ParserError($"Expecting a call to a function, got token instead: {Enumerator.Current}", CurrentPosition);
  219. return null;
  220. }
  221. Marker symbolMarker = CurrentPosition;
  222. string target = Lexer.Identifier;
  223. List<ExecutionNode> parameters = new List<ExecutionNode>();
  224. if (ProcedureDefinitions.All(x => !x.Name.Equals(target, StringComparison.OrdinalIgnoreCase)))
  225. {
  226. error = new ParserError($"Could not find procedure: {Lexer.Identifier}", CurrentPosition);
  227. return null;
  228. }
  229. Enumerator.MoveNext();
  230. while (Enumerator.Current != Token.NewLine
  231. && Enumerator.Current != Token.EOF
  232. && Enumerator.Current != Token.RParen)
  233. {
  234. parameters.Add(Expression(out error));
  235. if (error != null)
  236. {
  237. error = new ParserError($"{error.ErrorMessage} (target [{target}])", error.SymbolMarker);
  238. return null;
  239. }
  240. if (Enumerator.Current != Token.Comma
  241. && Enumerator.Current != Token.RParen
  242. && Enumerator.Current != Token.NewLine
  243. && Enumerator.Current != Token.EOF)
  244. {
  245. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  246. return null;
  247. }
  248. }
  249. if (Enumerator.Current == Token.RParen)
  250. Enumerator.MoveNext();
  251. if (Enumerator.Current != Token.NewLine
  252. && Enumerator.Current != Token.EOF)
  253. {
  254. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  255. return null;
  256. }
  257. return CallMethod(target, symbolMarker, parameters.ToArray());
  258. }
  259. else if (Lexer.Identifier.Equals("CALLFORM", StringComparison.OrdinalIgnoreCase)
  260. || Lexer.Identifier.Equals("TRYCALLFORM", StringComparison.OrdinalIgnoreCase)
  261. || Lexer.Identifier.Equals("TRYCCALLFORM", StringComparison.OrdinalIgnoreCase)
  262. || Lexer.Identifier.Equals("TRYJUMPFORM", StringComparison.OrdinalIgnoreCase))
  263. {
  264. string statementName = Lexer.Identifier;
  265. var node = new ExecutionNode
  266. {
  267. Type = "callform",
  268. Metadata =
  269. {
  270. ["try"] = statementName.StartsWith("TRY").ToString()
  271. },
  272. Symbol = CurrentPosition
  273. };
  274. ExecutionNode nameValue = null;
  275. List<ExecutionNode> parameters = new List<ExecutionNode>();
  276. Enumerator.MoveNext();
  277. do
  278. {
  279. ExecutionNode newValue = null;
  280. if (Enumerator.Current == Token.Identifer)
  281. {
  282. newValue = CreateConstant(Lexer.Identifier, CurrentPosition);
  283. }
  284. else if (Enumerator.Current == Token.OpenBracket)
  285. {
  286. newValue = Expression(out error);
  287. if (error != null)
  288. return null;
  289. }
  290. else if (Enumerator.Current == Token.LParen)
  291. {
  292. break;
  293. }
  294. else
  295. {
  296. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  297. return null;
  298. }
  299. nameValue = nameValue == null
  300. ? newValue
  301. : OperateNodes(nameValue, newValue, Token.Plus);
  302. Enumerator.MoveNext();
  303. } while (Enumerator.Current != Token.Comma
  304. && Enumerator.Current != Token.NewLine
  305. && Enumerator.Current != Token.EOF);
  306. while (Enumerator.Current != Token.NewLine
  307. && Enumerator.Current != Token.EOF
  308. && Enumerator.Current != Token.RParen)
  309. {
  310. parameters.Add(Expression(out error));
  311. if (error != null)
  312. {
  313. error = new ParserError($"{error.ErrorMessage} (statement [{statementName}])", error.SymbolMarker);
  314. return null;
  315. }
  316. if (Enumerator.Current != Token.Comma
  317. && Enumerator.Current != Token.NewLine
  318. && Enumerator.Current != Token.EOF
  319. && Enumerator.Current != Token.RParen)
  320. {
  321. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  322. return null;
  323. }
  324. }
  325. node.SubNodes = new List<ExecutionNode>
  326. {
  327. new ExecutionNode
  328. {
  329. Type = "name",
  330. SubNodes = new List<ExecutionNode> { nameValue }
  331. },
  332. new ExecutionNode
  333. {
  334. Type = "parameters",
  335. SubNodes = parameters
  336. },
  337. };
  338. return node;
  339. }
  340. else if (Lexer.Identifier.Equals("BEGIN", StringComparison.OrdinalIgnoreCase))
  341. {
  342. var node = new ExecutionNode
  343. {
  344. Type = "statement",
  345. Metadata =
  346. {
  347. ["name"] = "BEGIN"
  348. },
  349. Symbol = CurrentPosition
  350. };
  351. Enumerator.MoveNext();
  352. if (Enumerator.Current != Token.Identifer)
  353. {
  354. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  355. return null;
  356. }
  357. node.SubNodes = new List<ExecutionNode> { CreateConstant(Lexer.Identifier, CurrentPosition) };
  358. return node;
  359. }
  360. else //treat as statement
  361. {
  362. string statementName = Lexer.Identifier;
  363. var node = new ExecutionNode
  364. {
  365. Type = "statement",
  366. Metadata =
  367. {
  368. ["name"] = statementName
  369. },
  370. Symbol = CurrentPosition
  371. };
  372. List<ExecutionNode> parameters = new List<ExecutionNode>();
  373. Keyword keyword = ExplicitKeywords.FirstOrDefault(x => x.Name == statementName);
  374. if (keyword?.ImplicitString == true)
  375. {
  376. var value = ParseString(out error, true, keyword.ImplicitFormatted);
  377. if (error != null)
  378. return null;
  379. if (value != null)
  380. parameters.Add(value);
  381. node.SubNodes = parameters;
  382. return node;
  383. }
  384. if (GetNextToken(true) == Token.NewLine
  385. || GetNextToken(true) == Token.EOF)
  386. {
  387. return node;
  388. }
  389. if (GetNextToken(true) == Token.Colon
  390. || GetNextToken(true) == Token.Equal)
  391. {
  392. error = new ParserError($"Undeclared variable: {statementName}", node.Symbol);
  393. return null;
  394. }
  395. while (Enumerator.Current != Token.NewLine
  396. && Enumerator.Current != Token.EOF)
  397. {
  398. parameters.Add(Expression(out error));
  399. if (error != null)
  400. {
  401. error = new ParserError($"{error.ErrorMessage} (statement [{statementName}])", error.SymbolMarker);
  402. return null;
  403. }
  404. if (Enumerator.Current != Token.Comma
  405. && Enumerator.Current != Token.NewLine
  406. && Enumerator.Current != Token.EOF)
  407. {
  408. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  409. return null;
  410. }
  411. }
  412. node.SubNodes = parameters;
  413. return node;
  414. }
  415. case Token.GotoLabel:
  416. if (GetNextToken() != Token.Identifer)
  417. {
  418. error = new ParserError($"Expected an identifer, got {Enumerator.Current}", CurrentPosition);
  419. return null;
  420. }
  421. return new ExecutionNode
  422. {
  423. Type = "anchor",
  424. Anchor = Lexer.Identifier
  425. };
  426. case Token.AtSymbol:
  427. case Token.Sharp:
  428. while (Enumerator.MoveNext()
  429. && Enumerator.Current != Token.NewLine
  430. && Enumerator.Current != Token.EOF)
  431. {
  432. }
  433. return null;
  434. case Token.NewLine:
  435. case Token.EOF:
  436. return null;
  437. default:
  438. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  439. return null;
  440. }
  441. }
  442. protected bool IsVariable(string identifier)
  443. {
  444. return GlobalVariables.Any(x => x.Name.Equals(identifier, StringComparison.OrdinalIgnoreCase))
  445. || LocalVariables.Any(x => x.Name.Equals(identifier, StringComparison.OrdinalIgnoreCase))
  446. || ConstantDefinitions.Any(x => x.Name.Equals(identifier, StringComparison.OrdinalIgnoreCase));
  447. }
  448. protected ExecutionNode GetVariable(out ParserError error)
  449. {
  450. string variableName = Lexer.Identifier;
  451. Marker symbol = CurrentPosition;
  452. List<ExecutionNode> indices = new List<ExecutionNode>();
  453. error = null;
  454. while (GetNextToken(true) == Token.Colon)
  455. {
  456. GetNextToken();
  457. var token = GetNextToken();
  458. if (token == Token.LParen)
  459. {
  460. indices.Add(Expression(out error));
  461. if (error != null)
  462. return null;
  463. if (Enumerator.Current != Token.RParen)
  464. {
  465. error = new ParserError("Invalid expression - Expected right bracket", CurrentPosition);
  466. return null;
  467. }
  468. }
  469. else if (token == Token.Value)
  470. {
  471. indices.Add(CreateConstant(Lexer.Value, CurrentPosition));
  472. }
  473. else if (token == Token.Identifer)
  474. {
  475. if (CsvDefinition.VariableIndexDictionary.TryGetValue(variableName, out var varTable)
  476. && varTable.TryGetValue(Lexer.Identifier, out int index))
  477. {
  478. indices.Add(CreateConstant(index, CurrentPosition));
  479. continue;
  480. }
  481. if (IsVariable(Lexer.Identifier))
  482. {
  483. var subNode = new ExecutionNode
  484. {
  485. Type = "variable",
  486. Metadata =
  487. {
  488. ["name"] = Lexer.Identifier
  489. },
  490. Symbol = CurrentPosition
  491. };
  492. indices.Add(subNode);
  493. continue;
  494. }
  495. if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
  496. {
  497. indices.Add(GetFunction(out error));
  498. if (error != null)
  499. return null;
  500. continue;
  501. }
  502. error = new ParserError($"Unknown identifier: {Lexer.Identifier}", CurrentPosition);
  503. return null;
  504. }
  505. }
  506. return GetVariable(variableName, symbol, indices.ToArray());
  507. }
  508. protected ExecutionNode GetFunction(out ParserError error)
  509. {
  510. error = null;
  511. Marker symbolMarker = CurrentPosition;
  512. List<ExecutionNode> parameters = new List<ExecutionNode>();
  513. string functionName = Lexer.Identifier;
  514. if (GetNextToken() != Token.LParen)
  515. {
  516. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  517. return null;
  518. }
  519. while (Enumerator.Current == Token.Comma
  520. || Enumerator.Current == Token.LParen)
  521. {
  522. if (GetNextToken(true) == Token.RParen)
  523. break;
  524. if (GetNextToken(true) == Token.Comma)
  525. {
  526. var defaultValue = new ExecutionNode
  527. {
  528. Type = "defaultvalue",
  529. Symbol = CurrentPosition
  530. };
  531. parameters.Add(defaultValue);
  532. GetNextToken();
  533. continue;
  534. }
  535. parameters.Add(Expression(out error));
  536. if (error != null)
  537. return null;
  538. if (Enumerator.Current != Token.Comma
  539. && Enumerator.Current != Token.RParen)
  540. {
  541. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  542. return null;
  543. }
  544. }
  545. if (Enumerator.Current != Token.RParen)
  546. {
  547. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  548. return null;
  549. }
  550. if (hasPeeked)
  551. {
  552. GetNextToken();
  553. }
  554. var functionDefinition = FunctionDefinitions.FirstOrDefault(x => x.Name == functionName
  555. && (x.Parameters.Length >= parameters.Count
  556. || x.Parameters.Any(y => y.IsArrayParameter)));
  557. if (functionDefinition == null)
  558. {
  559. error = new ParserError($"No matching method with same amount of parameters: {functionName} ({parameters.Count})", CurrentPosition);
  560. return null;
  561. }
  562. return CallMethod(functionName, symbolMarker, parameters.ToArray());
  563. }
  564. private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
  565. {
  566. { Token.Or, 0 }, { Token.And, 0 },
  567. { Token.Equal, 1 }, { Token.NotEqual, 1 },
  568. { Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
  569. { Token.Plus, 2 }, { Token.Minus, 2 },
  570. { Token.Asterisk, 3 }, { Token.Slash, 3 }, { Token.Modulo, 3 },
  571. { Token.Caret, 4 }, { Token.ShiftLeft, 4 }, { Token.ShiftRight, 4 },
  572. { Token.Not, 5 },
  573. };
  574. protected ExecutionNode Expression(out ParserError error, bool useModulo = true, bool ternaryString = false)
  575. {
  576. error = null;
  577. var operators = new Stack<Token>();
  578. var operands = new Stack<ExecutionNode>();
  579. Token token;
  580. void ProcessOperation(out ParserError localError)
  581. {
  582. localError = null;
  583. Token op = operators.Pop();
  584. if (op.IsUnary() && operands.Count >= 1)
  585. {
  586. var operand = operands.Pop();
  587. operands.Push(new ExecutionNode
  588. {
  589. Type = "operation",
  590. Metadata =
  591. {
  592. ["type"] = GetOperationName(op),
  593. ["unary"] = "true"
  594. },
  595. SubNodes = new List<ExecutionNode>
  596. {
  597. operand
  598. }
  599. });
  600. }
  601. else if (!op.IsUnary() && operands.Count >= 2)
  602. {
  603. ExecutionNode right = operands.Pop();
  604. ExecutionNode left = operands.Pop();
  605. operands.Push(new ExecutionNode
  606. {
  607. Type = "operation",
  608. Metadata =
  609. {
  610. ["type"] = GetOperationName(op),
  611. ["unary"] = "false"
  612. },
  613. SubNodes = new List<ExecutionNode>
  614. {
  615. left,
  616. right
  617. }
  618. });
  619. }
  620. else
  621. localError = new ParserError("Invalid expression - not enough operands", CurrentPosition);
  622. }
  623. void AttemptUnaryConversion(out ParserError localError)
  624. {
  625. localError = null;
  626. while (operators.Count > 0
  627. && operators.Peek().IsUnary())
  628. {
  629. ProcessOperation(out localError);
  630. if (localError != null)
  631. return;
  632. }
  633. }
  634. while ((token = GetNextToken()) != Token.NewLine
  635. && token != Token.EOF
  636. && token != Token.Comma
  637. && token != Token.Colon
  638. && token != Token.To
  639. && token != Token.CloseBracket
  640. && token != Token.RParen
  641. && token != Token.QuestionMark
  642. && token != Token.Sharp
  643. && (!ternaryString || token != Token.TernaryEscape)
  644. && (useModulo || token != Token.Modulo))
  645. {
  646. if (token == Token.Value)
  647. {
  648. operands.Push(CreateConstant(Lexer.Value, CurrentPosition));
  649. AttemptUnaryConversion(out error);
  650. if (error != null)
  651. return null;
  652. }
  653. else if (token == Token.QuotationMark || token == Token.AtSymbol)
  654. {
  655. operands.Push(ParseString(out error, false, false));
  656. if (error != null)
  657. return null;
  658. }
  659. else if (token == Token.Identifer)
  660. {
  661. if (IsVariable(Lexer.Identifier))
  662. {
  663. operands.Push(GetVariable(out error));
  664. if (error != null)
  665. return null;
  666. }
  667. else if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
  668. {
  669. operands.Push(GetFunction(out error));
  670. if (error != null)
  671. return null;
  672. }
  673. else
  674. {
  675. Warnings.Add(new ParserError($"Unknown identifier: {Lexer.Identifier}", CurrentPosition));
  676. break;
  677. }
  678. }
  679. else if (token == Token.TernaryEscape)
  680. {
  681. operands.Push(Expression(out error, useModulo, true));
  682. if (error != null)
  683. return null;
  684. }
  685. else if (token.IsArithmetic())
  686. {
  687. if (!operands.Any() && token.IsUnary())
  688. {
  689. operators.Push(token);
  690. continue;
  691. }
  692. if (!operands.Any() && !token.IsUnary())
  693. {
  694. error = new ParserError($"Invalid unary operator: {token}", CurrentPosition);
  695. return null;
  696. }
  697. while (operators.Any() && OrderOfOps[token] <= OrderOfOps[operators.Peek()])
  698. {
  699. ProcessOperation(out error);
  700. if (error != null)
  701. return null;
  702. }
  703. operators.Push(token);
  704. }
  705. else if (token == Token.LParen)
  706. {
  707. operands.Push(Expression(out var localError));
  708. if (localError != null)
  709. {
  710. error = localError;
  711. return null;
  712. }
  713. }
  714. else if (token == Token.RParen)
  715. {
  716. break;
  717. }
  718. else
  719. {
  720. error = new ParserError($"Unexpected token: {token}", CurrentPosition);
  721. return null;
  722. }
  723. }
  724. while (operators.Any())
  725. {
  726. ProcessOperation(out error);
  727. if (error != null)
  728. return null;
  729. }
  730. if (!operands.Any())
  731. {
  732. error = new ParserError("Invalid expression - Empty operand stack", CurrentPosition);
  733. return null;
  734. }
  735. var result = operands.Pop();
  736. if (token != Token.QuestionMark)
  737. return result;
  738. var resultTrue = ternaryString ? ParseString(out error, useModulo, true, true) : Expression(out error, useModulo, false);
  739. if (error != null)
  740. return null;
  741. var resultFalse = ternaryString ? ParseString(out error, useModulo, true, true) : Expression(out error, useModulo, false);
  742. if (error != null)
  743. return null;
  744. return CallMethod("__INLINEIF", CurrentPosition, result, resultTrue, resultFalse);
  745. }
  746. protected ExecutionNode ParseString(out ParserError error, bool implicitString, bool canFormat = false, bool nestedTernary = false)
  747. {
  748. error = null;
  749. ExecutionNode value = null;
  750. if (Lexer.IsPeeking)
  751. Lexer.GetNextChar();
  752. if (nestedTernary && (Lexer.CurrentChar == '?' || Lexer.CurrentChar == '#'))
  753. Lexer.GetNextChar();
  754. if (!implicitString)
  755. {
  756. if (Lexer.CurrentChar == '@')
  757. {
  758. canFormat = true;
  759. Lexer.GetNextChar();
  760. }
  761. if (Lexer.CurrentChar == '"')
  762. {
  763. Lexer.GetNextChar();
  764. }
  765. }
  766. else
  767. {
  768. if (char.IsWhiteSpace(Lexer.CurrentChar) && Lexer.CurrentChar != '\n')
  769. Lexer.GetNextChar();
  770. }
  771. StringBuilder currentBlock = new StringBuilder();
  772. void commitBlock()
  773. {
  774. if (currentBlock.Length == 0)
  775. return;
  776. ExecutionNode stringBlock = CreateConstant(currentBlock.ToString(), CurrentPosition);
  777. value = value == null
  778. ? stringBlock
  779. : OperateNodes(value, stringBlock, Token.Plus);
  780. currentBlock.Clear();
  781. }
  782. while ((Lexer.CurrentChar != '"' || implicitString)
  783. && Lexer.CurrentChar != '\n'
  784. && Lexer.CurrentChar != '\0')
  785. {
  786. if (Lexer.CurrentChar == '\r')
  787. {
  788. Lexer.GetNextChar();
  789. continue;
  790. }
  791. if (nestedTernary && Lexer.CurrentChar == '#')
  792. break;
  793. if (canFormat && Lexer.CurrentChar == '\\')
  794. {
  795. Lexer.GetNextChar();
  796. if (Lexer.CurrentChar == '@')
  797. {
  798. if (nestedTernary)
  799. {
  800. Lexer.GetNextChar();
  801. break;
  802. }
  803. var expressionValue = Expression(out error, true, true);
  804. if (error != null)
  805. return null;
  806. commitBlock();
  807. value = value == null
  808. ? expressionValue
  809. : OperateNodes(value, expressionValue, Token.Plus);
  810. continue;
  811. }
  812. if (Lexer.CurrentChar == 'n')
  813. {
  814. currentBlock.Append('\n');
  815. Lexer.GetNextChar();
  816. continue;
  817. }
  818. currentBlock.Append(Lexer.CurrentChar);
  819. Lexer.GetNextChar();
  820. continue;
  821. }
  822. if (canFormat && (Lexer.CurrentChar == '{' || Lexer.CurrentChar == '%'))
  823. {
  824. bool useModulo = Lexer.CurrentChar != '%';
  825. List<ExecutionNode> formatParams = new List<ExecutionNode>();
  826. Marker symbolMarker = CurrentPosition;
  827. do
  828. {
  829. var expressionValue = Expression(out error, useModulo, nestedTernary);
  830. if (error != null)
  831. return null;
  832. formatParams.Add(expressionValue);
  833. } while (Enumerator.Current == Token.Comma);
  834. var formattedValue = CallMethod("__FORMAT", symbolMarker, formatParams.ToArray());
  835. commitBlock();
  836. value = value == null
  837. ? formattedValue
  838. : OperateNodes(value, formattedValue, Token.Plus);
  839. Lexer.GetNextChar();
  840. continue;
  841. }
  842. currentBlock.Append(Lexer.CurrentChar);
  843. Lexer.GetNextChar();
  844. }
  845. if (!nestedTernary && !implicitString && (Lexer.CurrentChar == '\0' || Lexer.CurrentChar == '\n'))
  846. {
  847. error = new ParserError("Was expecting string to be closed", CurrentPosition);
  848. return null;
  849. }
  850. commitBlock();
  851. value = value ?? CreateConstant("", CurrentPosition);
  852. return value;
  853. }
  854. private static readonly Dictionary<Token, string> OperationNames = new Dictionary<Token, string>
  855. {
  856. [Token.Plus] = "add",
  857. [Token.Asterisk] = "multiply",
  858. [Token.Minus] = "subtract",
  859. [Token.Slash] = "divide",
  860. };
  861. public static string GetOperationName(Token token)
  862. {
  863. return OperationNames.TryGetValue(token, out string result)
  864. ? result
  865. : token.ToString();
  866. }
  867. public static ExecutionNode CreateConstant(Value value, Marker symbolMarker)
  868. {
  869. return new ExecutionNode
  870. {
  871. Type = "constant",
  872. Metadata =
  873. {
  874. ["type"] = value.Type.ToString(),
  875. ["value"] = value.ToString()
  876. },
  877. Symbol = symbolMarker
  878. };
  879. }
  880. public static ExecutionNode OperateNodes(ExecutionNode left, ExecutionNode right, Token token)
  881. {
  882. return new ExecutionNode
  883. {
  884. Type = "operation",
  885. Metadata =
  886. {
  887. ["type"] = GetOperationName(token)
  888. },
  889. SubNodes = new List<ExecutionNode>
  890. {
  891. left,
  892. right
  893. }
  894. };
  895. }
  896. public static ExecutionNode CallMethod(string methodName, Marker symbolMarker, params ExecutionNode[] parameters)
  897. {
  898. return new ExecutionNode
  899. {
  900. Type = "call",
  901. Metadata =
  902. {
  903. ["target"] = methodName
  904. },
  905. Symbol = symbolMarker,
  906. SubNodes = new List<ExecutionNode>
  907. {
  908. new ExecutionNode
  909. {
  910. Type = "parameters",
  911. SubNodes = parameters.ToList()
  912. }
  913. }
  914. };
  915. }
  916. public static ExecutionNode GetVariable(string variableName, Marker marker, params ExecutionNode[] indexNodes)
  917. {
  918. var node = new ExecutionNode
  919. {
  920. Type = "variable",
  921. Metadata =
  922. {
  923. ["name"] = variableName
  924. },
  925. SubNodes = indexNodes.ToList(),
  926. Symbol = marker
  927. };
  928. if (indexNodes.Length > 0)
  929. node.SubNodes = new List<ExecutionNode>
  930. {
  931. new ExecutionNode
  932. {
  933. Type = "index",
  934. SubNodes = indexNodes.ToList()
  935. }
  936. };
  937. return node;
  938. }
  939. #endregion
  940. #region Post-processor
  941. protected void PostProcess(List<ExecutionNode> nodes)
  942. {
  943. Branchify(nodes);
  944. }
  945. #region Branching
  946. private class BranchType
  947. {
  948. public string nodeType;
  949. public string startStatement;
  950. public string endStatement;
  951. public BranchType(string nodeType, string startStatement, string endStatement)
  952. {
  953. this.nodeType = nodeType;
  954. this.startStatement = startStatement;
  955. this.endStatement = endStatement;
  956. }
  957. }
  958. private static BranchType[] DefinedBranches { get; } =
  959. {
  960. new BranchType("for", "FOR", "NEXT"),
  961. new BranchType("do", "DO", "LOOP"),
  962. new BranchType("select", "SELECTCASE", "ENDSELECT"),
  963. new BranchType("case", "CASE", null),
  964. };
  965. protected static void Branchify(List<ExecutionNode> nodes)
  966. {
  967. Stack<KeyValuePair<BranchType, ExecutionNode>> currentNodeStack = new Stack<KeyValuePair<BranchType, ExecutionNode>>();
  968. foreach (var node in nodes)
  969. {
  970. var branchType = DefinedBranches.FirstOrDefault(x => node.Type == "statement" && node["name"].Equals(x.startStatement, StringComparison.OrdinalIgnoreCase));
  971. if (branchType != null)
  972. currentNodeStack.Push(new KeyValuePair<BranchType, ExecutionNode>(branchType, node));
  973. }
  974. foreach (var nodePair in currentNodeStack)
  975. {
  976. var node = nodePair.Value;
  977. var branchType = nodePair.Key;
  978. int index = nodes.IndexOf(node);
  979. int endIndex = 0;
  980. for (int i = index; i < nodes.Count; i++)
  981. {
  982. var endNode = nodes[i];
  983. if (endNode.Type == "statement" && endNode["name"].Equals(nodePair.Key.endStatement, StringComparison.OrdinalIgnoreCase))
  984. {
  985. endIndex = i;
  986. break;
  987. }
  988. }
  989. if (endIndex == 0)
  990. throw new ParserException($"Could not find matching '{branchType.endStatement}' for '{branchType.startStatement}' statement (branch type '{branchType.nodeType}')");
  991. List<ExecutionNode> subNodes = new List<ExecutionNode>();
  992. var startContextNode = node;
  993. startContextNode.Type = "start-context";
  994. subNodes.Add(startContextNode);
  995. var endContextNode = nodes[endIndex];
  996. endContextNode.Type = "end-context";
  997. subNodes.Add(endContextNode);
  998. subNodes.AddRange(nodes.Skip(index + 1).Take(endIndex - index - 1));
  999. nodes.RemoveRange(index, (endIndex - index) + 1);
  1000. ExecutionNode newNode = new ExecutionNode
  1001. {
  1002. Type = branchType.nodeType,
  1003. SubNodes = subNodes
  1004. };
  1005. nodes.Insert(index, newNode);
  1006. }
  1007. }
  1008. #endregion
  1009. #endregion
  1010. }
  1011. }