Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. /* description: Parses end executes JediScript expressions. */
  2.  
  3. /* lexical grammar */
  4. %lex
  5. %x DoubleQuotedString
  6. %x SingleQuotedString
  7. %x QuotedStringEscape
  8. %%
  9.  
  10.  
  11. \/\/([^\n\r]*) /* skip single-line comments */
  12. \/\*([\u0000-\uffff]*?)\*\/ /* skip multi-line comments */
  13. \s+ /* skip whitespace */
  14.  
  15. "function" return 'function'
  16. "return" return 'return'
  17. "if" return 'if'
  18. "else" return 'else'
  19. "while" return 'while'
  20. "for" return 'for'
  21. "break" return 'break'
  22. "continue" return 'continue'
  23. "let" return 'let'
  24. "const" return 'const'
  25. "===" return '==='
  26. "=>" return '=>'
  27. "=" return '='
  28. "{" return '{'
  29. "}" return '}'
  30. ";" return ';'
  31. "," return ','
  32. "true" return 'true'
  33. "false" return 'false'
  34. "NaN" return 'NaN'
  35. "Infinity" return 'Infinity'
  36. "null" return 'emptylist'
  37. "[" return '['
  38. "]" return ']'
  39.  
  40. '""' return 'EmptyString'
  41. "''" return 'EmptyString'
  42. '"' this.begin('DoubleQuotedString');
  43. "'" this.begin('SingleQuotedString');
  44. <DoubleQuotedString,SingleQuotedString>\\ this.begin('QuotedStringEscape');
  45. <DoubleQuotedString>'"' this.popState();
  46. <SingleQuotedString>"'" this.popState();
  47. <QuotedStringEscape>(.|\r\n|\n) { this.popState(); return 'QuotedStringEscape'; } /* The newlines are there because we can span strings across lines using \ */
  48. <DoubleQuotedString>[^"\\]* return 'QuotedString';
  49. <SingleQuotedString>[^'\\]* return 'QuotedString';
  50.  
  51.  
  52. [A-Za-z_][A-Za-z0-9_]* return 'Identifier' /* TODO: non-ASCII identifiers */
  53.  
  54. [0-9]+("."[0-9]+)?([eE][\-+]?[0-9]+)?\b return 'FLOAT_NUMBER' /* 3.1, 3.1e-7 */
  55. [0-9]+\b return 'INT_NUMBER'
  56.  
  57. "+" return '+'
  58. "-" return '-'
  59. "*" return '*'
  60. "/" return '/'
  61. "%" return '%'
  62. "!==" return '!=='
  63. "<=" return '<='
  64. ">=" return '>='
  65. "<" return '<'
  66. ">" return '>'
  67. "!" return '!'
  68. "&&" return '&&'
  69. "||" return '||'
  70. "(" return '('
  71. ")" return ')'
  72. "?" return '?'
  73. ":" return ':'
  74.  
  75. <<EOF>> return 'EOF'
  76. . return 'INVALID'
  77.  
  78. /lex
  79.  
  80. /* operator associations and precedence */
  81.  
  82. %left ';'
  83. %right '='
  84. %left '=>' ARROW
  85. %right '?' ':'
  86. %left '||'
  87. %left '&&'
  88. %left '===' '!=='
  89. %left '<' '>' '<=' '>='
  90. %left '+' '-'
  91. %left '*' '/' '%'
  92. %right '!' UMINUS UPLUS
  93. %left '[' ']'
  94. %left '.'
  95.  
  96. %% /* language grammar */
  97.  
  98. program
  99. : statements EOF
  100. { return $1; }
  101. ;
  102.  
  103. statements
  104. :
  105. { $$ = ""; }
  106. | statement statements
  107. { $$ = $1 + $2; }
  108. ;
  109.  
  110. statement
  111. :
  112. ifstatement
  113.  
  114. | whilestatement
  115.  
  116. | forstatement
  117.  
  118. | 'function ' identifier '(' identifiers ')' '{' statements '}'
  119. {{
  120. $$ = "function" + $2 + "(" + $4 + ") {" + $7 + "}" ;
  121. }}
  122. | constdeclaration
  123. | letdeclaration
  124. | '{' statements '}'
  125. {{
  126. $$ = "{" + $2 + "}";
  127. }}
  128.  
  129. | assignment ';'
  130. {{
  131. $$ = $1 + ";";
  132. }}
  133.  
  134. | expression ';'
  135. {{
  136. $$ = $1 + ";";
  137. }}
  138. | 'return' expression ';'
  139. {{
  140. $$ = "return " + $2 + ";";
  141. }}
  142.  
  143.  
  144. | break ';'
  145. {{
  146. $$ = "break;";
  147. }}
  148. | continue ';'
  149. {{
  150. $$ = "continue;";
  151. }}
  152.  
  153. ;
  154.  
  155. letdeclaration
  156. :
  157. 'let' identifier '=' expression ';'
  158. {{
  159. $$ = "var " + $2 + "=" + $4 + ";";
  160. }}
  161. ;
  162.  
  163. constdeclaration
  164. :
  165. 'const' identifier '=' expression ';'
  166. {{
  167. $$ = "var " + $2 + "=" + $4 + ";";
  168. }}
  169. ;
  170.  
  171.  
  172. assignment
  173. :
  174. expression '=' expression
  175. {{
  176. $$ = $1 + "=" + $3;
  177. }}
  178. ;
  179.  
  180. ifstatement
  181. :
  182. 'if' '(' expression ')' '{' statements '}' 'else' '{' statements '}'
  183. {{
  184. $$ = "if (" + $3 + ") {" + $6 + "} else {" + $10 + "}";
  185. }}
  186. | 'if' '(' expression ')' '{' statements '}' 'else' ifstatement
  187. {{
  188. $$ = "if (" + $3 + ") {" + $6 + "} else " + $9;
  189. }}
  190. ;
  191.  
  192.  
  193. whilestatement
  194. :
  195. 'while' '(' expression ')' '{' statements '}'
  196. {{
  197. $$ = "while (" + $3 + ") {" + $6 + "}";
  198. }}
  199. ;
  200.  
  201. forstatement
  202. :
  203. 'for' '(' forinitialiser expression ';' forfinaliser ')' '{' statements '}'
  204. {{
  205. $$ = "for (" + $3 + $4 + ";" + $6 + ") {" + $9 + "}";
  206. }}
  207. ;
  208.  
  209. forinitialiser
  210. :
  211. letdeclaration
  212. | assignment ';'
  213. {{
  214. $$ = $1 + ";";
  215. }}
  216. ;
  217.  
  218. forfinaliser
  219. :
  220. assignment
  221. ;
  222.  
  223.  
  224. expression
  225. :
  226. expression '+' expression
  227. {{
  228. $$ = $1 + "+" + $3;
  229. }}
  230. | expression '-' expression
  231. {{
  232. $$ = $1 + "-" + $3;
  233. }}
  234. | expression '*' expression
  235. {{
  236. $$ = $1 + "*" + $3;
  237. }}
  238. | expression '/' expression
  239. {{
  240. $$ = $1 + "/" + $3;
  241. }}
  242. | expression '%' expression
  243. {{
  244. $$ = $1 + "%" + $3;
  245. }}
  246. | '-' expression %prec UMINUS
  247. {{
  248. $$ = "-" + $1;
  249. }}
  250. | '+' expression %prec UPLUS
  251. {{
  252. $$ = "+" + $1;
  253. }}
  254. | '!' expression
  255. {{
  256. $$ = "!" + $1;
  257. }}
  258. | expression '&&' expression
  259. {{
  260. $$ = $1 + "&&" + $3;
  261. }}
  262. | expression '||' expression
  263. {{
  264. $$ = $1 + "||" + $3;
  265. }}
  266. | expression '===' expression
  267. {{
  268. $$ = $1 + "===" + $3;
  269. }}
  270. | expression '!==' expression
  271. {{
  272. $$ = $1 + "!==" + $3;
  273. }}
  274. | expression '>' expression
  275. {{
  276. $$ = $1 + ">" + $3;
  277. }}
  278. | expression '<' expression
  279. {{
  280. $$ = $1 + "<" + $3;
  281. }}
  282. | expression '>=' expression
  283. {{
  284. $$ = $1 + ">=" + $3;
  285. }}
  286. | expression '<=' expression
  287. {{
  288. $$ = $1 + "<=" + $3;
  289. }}
  290. | '(' identifiers ')' '=>' expression %prec ARROW
  291. {{
  292. $$ = "(function(" + $2 + "){ return " + $5 + ";})";
  293. }}
  294. | '(' identifiers ')' '=>' '{' statements '}' %prec ARROW
  295. {{
  296. $$ = "(function(" + $2 + "){ " + $6 + "})";
  297. }}
  298. | identifier '=>' expression
  299. {{
  300. $$ = "(function(" + $1 + "){ return " + $3 + ";})";
  301. }}
  302. | identifier '=>' '{' statements '}'
  303. {{
  304. $$ = "(function(" + $1 + "){ " + $4 + "})";
  305. }}
  306.  
  307. | expression '[' expression ']'
  308. {{
  309. $$ = $1 + "[" + $3 + "]"
  310. }}
  311.  
  312. | '(' expression ')'
  313. {$$ = "(" + $2 + ")";}
  314.  
  315. | constants
  316. { $$ = $1; }
  317.  
  318. | identifier
  319. { $$ = $1; }
  320.  
  321. | '(' expression ')' '(' expressions ')'
  322. {{
  323. $$ = "(" + $2 + ")(" + $5 + ")";
  324. }}
  325.  
  326. | '[' expressions ']'
  327. {{
  328. $$ = "[" + $2 + "]";
  329. }}
  330.  
  331. | identifier '(' expressions ')'
  332. {{
  333. $$ = $1 + "(" + $3 + ")";
  334. }}
  335.  
  336. | expression '?' expression ':' expression
  337. {{
  338. $$ = $1 + "?" + $3 + ":" + $5;
  339. }}
  340. ;
  341.  
  342. constants
  343. :
  344. 'FLOAT_NUMBER'
  345. { $$ = String(parseFloat(yytext)); }
  346.  
  347. | 'INT_NUMBER'
  348. { $$ = String(parseInt(yytext, 10)); }
  349.  
  350. | 'true'
  351. { $$ = 'true'; }
  352.  
  353. | 'false'
  354. { $$ = 'false'; }
  355.  
  356. | 'NaN'
  357. { $$ = 'NaN'; }
  358.  
  359. | 'Infinity'
  360. { $$ = 'Infinity'; }
  361.  
  362. | quotedstring
  363.  
  364. | 'emptylist'
  365. { $$ = 'null'; }
  366. ;
  367.  
  368. quotedstring
  369. :
  370. 'EmptyString'
  371. {
  372. $$ = '""';
  373. }
  374. | 'QuotedString'
  375. | 'QuotedStringEscape'
  376. {
  377. switch (yytext)
  378. {
  379. case 'b': $$ = '\b'; break;
  380. case 'n': $$ = '\n'; break;
  381. case 'r': $$ = '\r'; break;
  382. case 't': $$ = '\t'; break;
  383. case "'": $$ = "'"; break;
  384. case '"': $$ = '"'; break;
  385. case '\\': $$ = '\\'; break;
  386. case '\n':
  387. case '\r\n': $$ = ''; break;
  388. default: $$ = '\\' + $1; break;
  389. }
  390. }
  391. | 'QuotedStringEscape' quotedstring
  392. {
  393. switch ($1)
  394. {
  395. case 'b': $$ = '\b'; break;
  396. case 'n': $$ = '\n'; break;
  397. case 'r': $$ = '\r'; break;
  398. case 't': $$ = '\t'; break;
  399. case "'": $$ = "'"; break;
  400. case '"': $$ = '"'; break;
  401. case '\\': $$ = '\\'; break;
  402. case '\n':
  403. case '\r\n': $$ = ''; break;
  404. default: $$ = '\\' + $1; break;
  405. }
  406. $$ += $2;
  407. }
  408. | 'QuotedString' quotedstring
  409. {
  410. $$ = $1 + $2;
  411. }
  412. ;
  413.  
  414. expressions
  415. :
  416. nonemptyexpressions
  417. { $$ = $1; }
  418. | /* NOTHING */
  419. { $$ = ""; }
  420. ;
  421.  
  422. nonemptyexpressions
  423. :
  424. expression ',' nonemptyexpressions
  425. { $$ = $1 + "," + $3; }
  426. | expression
  427. { $$ = $1; }
  428. ;
  429.  
  430. identifiers
  431. :
  432. nonemptyidentifiers
  433. { $$ = $1; }
  434. | /* NOTHING */
  435. { $$ = ""; }
  436. ;
  437.  
  438. nonemptyidentifiers
  439. :
  440. identifier ',' nonemptyidentifiers
  441. { $$ = $1 + "," + $3; }
  442. | identifier
  443. { $$ = $1; }
  444. ;
  445.  
  446. identifier
  447. :
  448. 'Identifier'
  449. {{
  450. $$ = yytext;
  451. }}
  452. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement