Hex Artifact Content
Not logged in

Artifact de91c770214a43a1c014c35e6dfa49b7d931de78:


0000: ef bb bf 2f 2a 2a 0a 20 2a 20 41 75 74 68 6f 72  .../**. * Author
0010: 73 3a 20 6b 2e 69 6e 61 62 61 0a 20 2a 20 4c 69  s: k.inaba. * Li
0020: 63 65 6e 73 65 3a 20 4e 59 53 4c 20 30 2e 39 39  cense: NYSL 0.99
0030: 38 32 20 68 74 74 70 3a 2f 2f 77 77 77 2e 6b 6d  82 http://www.km
0040: 6f 6e 6f 73 2e 6e 65 74 2f 6e 79 73 6c 2f 0a 20  onos.net/nysl/. 
0050: 2a 0a 20 2a 20 50 61 72 73 65 72 20 66 6f 72 20  *. * Parser for 
0060: 50 6f 6c 65 6d 79 20 70 72 6f 67 72 61 6d 6d 69  Polemy programmi
0070: 6e 67 20 6c 61 6e 67 75 61 67 65 0a 20 2a 2f 0a  ng language. */.
0080: 6d 6f 64 75 6c 65 20 70 6f 6c 65 6d 79 2e 70 61  module polemy.pa
0090: 72 73 65 3b 0a 69 6d 70 6f 72 74 20 70 6f 6c 65  rse;.import pole
00a0: 6d 79 2e 5f 63 6f 6d 6d 6f 6e 3b 0a 69 6d 70 6f  my._common;.impo
00b0: 72 74 20 70 6f 6c 65 6d 79 2e 6c 65 78 3b 0a 69  rt polemy.lex;.i
00c0: 6d 70 6f 72 74 20 70 6f 6c 65 6d 79 2e 61 73 74  mport polemy.ast
00d0: 3b 0a 0a 2f 2f 2f 20 50 61 72 73 69 6e 67 20 46  ;../// Parsing F
00e0: 61 69 6c 75 72 65 0a 0a 63 6c 61 73 73 20 50 61  ailure..class Pa
00f0: 72 73 65 72 45 78 63 65 70 74 69 6f 6e 20 3a 20  rserException : 
0100: 45 78 63 65 70 74 69 6f 6e 0a 7b 0a 70 72 69 76  Exception.{.priv
0110: 61 74 65 3a 0a 09 74 68 69 73 28 73 74 72 69 6e  ate:..this(strin
0120: 67 20 6d 73 67 29 20 7b 20 73 75 70 65 72 28 6d  g msg) { super(m
0130: 73 67 29 3b 20 7d 0a 09 73 74 61 74 69 63 20 50  sg); }..static P
0140: 61 72 73 65 72 45 78 63 65 70 74 69 6f 6e 20 63  arserException c
0150: 72 65 61 74 65 28 4c 65 78 65 72 29 28 4c 65 78  reate(Lexer)(Lex
0160: 65 72 20 6c 65 78 2c 20 73 74 72 69 6e 67 20 6d  er lex, string m
0170: 73 67 29 0a 09 7b 0a 09 09 72 65 74 75 72 6e 20  sg)..{...return 
0180: 6e 65 77 20 50 61 72 73 65 72 45 78 63 65 70 74  new ParserExcept
0190: 69 6f 6e 28 73 70 72 69 6e 74 66 21 22 5b 25 73  ion(sprintf!"[%s
01a0: 5d 20 25 73 22 28 0a 09 09 09 6c 65 78 2e 65 6d  ] %s"(....lex.em
01b0: 70 74 79 20 3f 20 22 45 4f 46 22 20 3a 20 74 6f  pty ? "EOF" : to
01c0: 21 73 74 72 69 6e 67 28 6c 65 78 2e 66 72 6f 6e  !string(lex.fron
01d0: 74 2e 70 6f 73 29 2c 20 6d 73 67 29 29 3b 0a 09  t.pos), msg));..
01e0: 7d 0a 7d 0a 0a 2f 2f 2f 20 4e 61 6d 65 64 20 43  }.}../// Named C
01f0: 6f 6e 73 74 72 75 63 74 6f 72 20 6f 66 20 50 61  onstructor of Pa
0200: 72 73 65 72 0a 0a 61 75 74 6f 20 70 61 72 73 65  rser..auto parse
0210: 72 46 72 6f 6d 4c 65 78 65 72 28 4c 65 78 65 72  rFromLexer(Lexer
0220: 29 28 4c 65 78 65 72 20 6c 65 78 29 0a 7b 0a 09  )(Lexer lex).{..
0230: 72 65 74 75 72 6e 20 6e 65 77 20 50 61 72 73 65  return new Parse
0240: 72 21 4c 65 78 65 72 28 6c 65 78 29 3b 0a 7d 0a  r!Lexer(lex);.}.
0250: 0a 2f 2f 2f 20 4e 61 6d 65 64 20 43 6f 6e 73 74  ./// Named Const
0260: 72 75 63 74 6f 72 20 6f 66 20 50 61 72 73 65 72  ructor of Parser
0270: 20 28 6a 75 73 74 20 66 77 64 20 74 6f 20 6c 65   (just fwd to le
0280: 78 65 72 46 72 6f 6d 53 74 72 69 6e 67 29 0a 0a  xerFromString)..
0290: 61 75 74 6f 20 70 61 72 73 65 72 46 72 6f 6d 53  auto parserFromS
02a0: 74 72 69 6e 67 28 54 2e 2e 2e 29 28 54 20 70 61  tring(T...)(T pa
02b0: 72 61 6d 73 29 0a 7b 0a 09 72 65 74 75 72 6e 20  rams).{..return 
02c0: 70 61 72 73 65 72 46 72 6f 6d 4c 65 78 65 72 28  parserFromLexer(
02d0: 70 6f 6c 65 6d 79 2e 6c 65 78 2e 6c 65 78 65 72  polemy.lex.lexer
02e0: 46 72 6f 6d 53 74 72 69 6e 67 28 70 61 72 61 6d  FromString(param
02f0: 73 29 29 3b 0a 7d 0a 0a 2f 2f 2f 20 4e 61 6d 65  s));.}../// Name
0300: 64 20 43 6f 6e 73 74 72 75 63 74 6f 72 20 6f 66  d Constructor of
0310: 20 50 61 72 73 65 72 20 28 6a 75 73 74 20 66 77   Parser (just fw
0320: 64 20 74 6f 20 6c 65 78 65 72 46 72 6f 6d 46 69  d to lexerFromFi
0330: 6c 65 29 0a 0a 61 75 74 6f 20 70 61 72 73 65 72  le)..auto parser
0340: 46 72 6f 6d 46 69 6c 65 28 54 2e 2e 2e 29 28 54  FromFile(T...)(T
0350: 20 70 61 72 61 6d 73 29 0a 7b 0a 09 72 65 74 75   params).{..retu
0360: 72 6e 20 70 61 72 73 65 72 46 72 6f 6d 4c 65 78  rn parserFromLex
0370: 65 72 28 70 6f 6c 65 6d 79 2e 6c 65 78 2e 6c 65  er(polemy.lex.le
0380: 78 65 72 46 72 6f 6d 46 69 6c 65 28 70 61 72 61  xerFromFile(para
0390: 6d 73 29 29 3b 0a 7d 0a 0a 2f 2f 2f 20 50 61 72  ms));.}../// Par
03a0: 73 65 72 0a 0a 63 6c 61 73 73 20 50 61 72 73 65  ser..class Parse
03b0: 72 28 4c 65 78 65 72 29 0a 7b 0a 09 74 68 69 73  r(Lexer).{..this
03c0: 28 4c 65 78 65 72 20 6c 65 78 29 0a 09 7b 0a 09  (Lexer lex)..{..
03d0: 09 74 68 69 73 2e 6c 65 78 20 3d 20 6c 65 78 3b  .this.lex = lex;
03e0: 0a 09 7d 0a 0a 09 50 72 6f 67 72 61 6d 20 70 61  ..}...Program pa
03f0: 72 73 65 50 72 6f 67 72 61 6d 28 29 0a 09 7b 0a  rseProgram()..{.
0400: 09 09 50 72 6f 67 72 61 6d 20 70 20 3d 20 70 61  ..Program p = pa
0410: 72 73 65 53 74 61 74 65 6d 65 6e 74 73 28 29 3b  rseStatements();
0420: 0a 09 09 69 66 28 20 21 6c 65 78 2e 65 6d 70 74  ...if( !lex.empt
0430: 79 20 29 20 7b 0a 09 09 09 61 75 74 6f 20 65 20  y ) {....auto e 
0440: 3d 20 50 61 72 73 65 72 45 78 63 65 70 74 69 6f  = ParserExceptio
0450: 6e 2e 63 72 65 61 74 65 28 6c 65 78 2c 20 22 63  n.create(lex, "c
0460: 61 6e 6e 6f 74 20 72 65 61 63 68 20 65 6f 66 22  annot reach eof"
0470: 29 3b 0a 09 09 09 74 68 72 6f 77 20 65 3b 0a 09  );....throw e;..
0480: 09 7d 0a 09 09 72 65 74 75 72 6e 20 70 3b 0a 09  .}...return p;..
0490: 7d 0a 0a 09 50 72 6f 67 72 61 6d 20 70 61 72 73  }...Program pars
04a0: 65 53 74 61 74 65 6d 65 6e 74 73 28 29 0a 09 7b  eStatements()..{
04b0: 0a 09 09 50 72 6f 67 72 61 6d 20 70 3b 0a 09 09  ...Program p;...
04c0: 77 68 69 6c 65 28 20 21 6c 65 78 2e 65 6d 70 74  while( !lex.empt
04d0: 79 20 26 26 20 28 6c 65 78 2e 66 72 6f 6e 74 2e  y && (lex.front.
04e0: 71 75 6f 74 65 64 20 7c 7c 20 6c 65 78 2e 66 72  quoted || lex.fr
04f0: 6f 6e 74 2e 73 74 72 21 3d 22 7d 22 29 20 29 0a  ont.str!="}") ).
0500: 09 09 09 70 20 7e 3d 20 70 61 72 73 65 53 74 61  ...p ~= parseSta
0510: 74 65 6d 65 6e 74 28 29 3b 0a 09 09 72 65 74 75  tement();...retu
0520: 72 6e 20 70 3b 0a 09 7d 0a 09 0a 09 53 74 61 74  rn p;..}....Stat
0530: 65 6d 65 6e 74 20 70 61 72 73 65 53 74 61 74 65  ement parseState
0540: 6d 65 6e 74 28 29 0a 09 7b 0a 09 09 61 75 74 6f  ment()..{...auto
0550: 20 73 61 76 65 64 20 3d 20 6c 65 78 2e 73 61 76   saved = lex.sav
0560: 65 3b 0a 09 09 73 63 6f 70 65 28 66 61 69 6c 75  e;...scope(failu
0570: 72 65 29 20 6c 65 78 20 3d 20 73 61 76 65 64 3b  re) lex = saved;
0580: 0a 0a 09 09 69 66 28 20 6c 65 78 2e 65 6d 70 74  ....if( lex.empt
0590: 79 20 29 0a 09 09 09 74 68 72 6f 77 20 6e 65 77  y )....throw new
05a0: 20 50 61 72 73 65 72 45 78 63 65 70 74 69 6f 6e   ParserException
05b0: 28 22 45 4f 46 20 64 75 72 69 6e 67 20 70 61 72  ("EOF during par
05c0: 73 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74  sing a statement
05d0: 22 29 3b 0a 09 09 61 75 74 6f 20 70 6f 73 20 3d  ");...auto pos =
05e0: 20 6c 65 78 2e 66 72 6f 6e 74 2e 70 6f 73 3b 0a   lex.front.pos;.
05f0: 0a 09 09 69 66 28 20 21 6c 65 78 2e 66 72 6f 6e  ...if( !lex.fron
0600: 74 2e 71 75 6f 74 65 64 20 26 26 20 6c 65 78 2e  t.quoted && lex.
0610: 66 72 6f 6e 74 2e 73 74 72 3d 3d 22 76 61 72 22  front.str=="var"
0620: 20 29 0a 09 09 7b 0a 09 09 09 2f 2f 20 22 76 61   )...{....// "va
0630: 72 22 20 56 61 72 20 22 3d 22 20 45 78 70 72 65  r" Var "=" Expre
0640: 73 73 69 6f 6e 20 22 3b 22 0a 09 09 09 6c 65 78  ssion ";"....lex
0650: 2e 70 6f 70 46 72 6f 6e 74 3b 0a 09 09 09 73 74  .popFront;....st
0660: 72 69 6e 67 20 76 61 72 20 3d 20 6c 65 78 2e 66  ring var = lex.f
0670: 72 6f 6e 74 2e 73 74 72 3b 0a 09 09 09 6c 65 78  ront.str;....lex
0680: 2e 70 6f 70 46 72 6f 6e 74 3b 0a 09 09 09 65 61  .popFront;....ea
0690: 74 28 22 3d 22 2c 20 22 66 6f 72 20 76 61 72 69  t("=", "for vari
06a0: 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e  able declaration
06b0: 22 29 3b 0a 09 09 09 61 75 74 6f 20 70 61 72 73  ");....auto pars
06c0: 65 64 20 3d 20 6e 65 77 20 44 65 63 6c 53 74 61  ed = new DeclSta
06d0: 74 65 6d 65 6e 74 28 70 6f 73 2c 20 76 61 72 2c  tement(pos, var,
06e0: 20 70 61 72 73 65 45 78 70 72 65 73 73 69 6f 6e   parseExpression
06f0: 28 29 29 3b 0a 09 09 09 65 61 74 28 22 3b 22 2c  ());....eat(";",
0700: 20 22 61 66 74 65 72 20 76 61 72 69 61 62 6c 65   "after variable
0710: 20 64 65 63 6c 61 72 61 74 69 6f 6e 22 29 3b 0a   declaration");.
0720: 09 09 09 72 65 74 75 72 6e 20 70 61 72 73 65 64  ...return parsed
0730: 3b 0a 09 09 7d 0a 09 09 65 6c 73 65 0a 09 09 7b  ;...}...else...{
0740: 0a 09 09 09 2f 2f 20 45 78 70 72 65 73 73 69 6f  ....// Expressio
0750: 6e 20 22 3b 22 0a 09 09 09 61 75 74 6f 20 70 61  n ";"....auto pa
0760: 72 73 65 64 20 3d 20 6e 65 77 20 45 78 70 72 53  rsed = new ExprS
0770: 74 61 74 65 6d 65 6e 74 28 70 6f 73 2c 20 70 61  tatement(pos, pa
0780: 72 73 65 45 78 70 72 65 73 73 69 6f 6e 28 29 29  rseExpression())
0790: 3b 0a 09 09 09 65 61 74 28 22 3b 22 2c 20 22 61  ;....eat(";", "a
07a0: 66 74 65 72 20 73 74 61 74 65 6d 65 6e 74 22 29  fter statement")
07b0: 3b 0a 09 09 09 72 65 74 75 72 6e 20 70 61 72 73  ;....return pars
07c0: 65 64 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 45 78 70  ed;...}..}...Exp
07d0: 72 65 73 73 69 6f 6e 20 70 61 72 73 65 45 78 70  ression parseExp
07e0: 72 65 73 73 69 6f 6e 28 29 0a 09 7b 0a 09 09 61  ression()..{...a
07f0: 75 74 6f 20 73 61 76 65 64 20 3d 20 6c 65 78 2e  uto saved = lex.
0800: 73 61 76 65 3b 0a 09 09 73 63 6f 70 65 28 66 61  save;...scope(fa
0810: 69 6c 75 72 65 29 20 6c 65 78 20 3d 20 73 61 76  ilure) lex = sav
0820: 65 64 3b 0a 09 09 72 65 74 75 72 6e 20 70 61 72  ed;...return par
0830: 73 65 45 28 30 29 3b 0a 09 7d 0a 0a 09 2f 2f 20  seE(0);..}...// 
0840: 5b 54 4f 44 4f 5d 20 6d 75 6c 74 69 2d 63 68 61  [TODO] multi-cha
0850: 72 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65 20  r operators are 
0860: 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 62 79  not supported by
0870: 20 74 68 65 20 6c 65 78 65 72 2e 2e 2e 0a 09 73   the lexer.....s
0880: 74 61 74 69 63 20 69 6d 6d 75 74 61 62 6c 65 20  tatic immutable 
0890: 73 74 72 69 6e 67 5b 5d 5b 5d 20 6f 70 65 72 61  string[][] opera
08a0: 74 6f 72 5f 70 65 72 66 65 72 65 6e 63 65 73 20  tor_perferences 
08b0: 3d 20 5b 0a 09 09 5b 22 3d 22 5d 2c 0a 09 09 5b  = [...["="],...[
08c0: 22 6f 72 22 5d 2c 0a 09 09 5b 22 61 6e 64 22 5d  "or"],...["and"]
08d0: 2c 0a 09 09 5b 22 21 3d 22 5d 2c 0a 09 09 5b 22  ,...["!="],...["
08e0: 3d 3d 22 5d 2c 0a 09 09 5b 22 3c 22 2c 22 3c 3d  =="],...["<","<=
08f0: 22 2c 22 3e 22 2c 22 3e 3d 22 5d 2c 0a 09 09 5b  ",">",">="],...[
0900: 22 7c 22 5d 2c 0a 09 09 5b 22 5e 22 5d 2c 0a 09  "|"],...["^"],..
0910: 09 5b 22 26 22 5d 2c 0a 09 09 5b 22 3c 3c 22 2c  .["&"],...["<<",
0920: 20 22 3e 3e 22 5d 2c 0a 09 09 5b 22 2b 22 2c 22   ">>"],...["+","
0930: 2d 22 5d 2c 0a 09 09 5b 22 2a 22 2c 22 2f 22 2c  -"],...["*","/",
0940: 22 25 22 5d 0a 09 5d 3b 0a 0a 09 45 78 70 72 65  "%"]..];...Expre
0950: 73 73 69 6f 6e 20 70 61 72 73 65 45 28 69 6e 74  ssion parseE(int
0960: 20 6c 65 76 65 6c 20 3d 20 30 29 0a 09 7b 0a 09   level = 0)..{..
0970: 09 69 66 28 20 6f 70 65 72 61 74 6f 72 5f 70 65  .if( operator_pe
0980: 72 66 65 72 65 6e 63 65 73 2e 6c 65 6e 67 74 68  rferences.length
0990: 20 3c 3d 20 6c 65 76 65 6c 20 29 0a 09 09 09 72   <= level )....r
09a0: 65 74 75 72 6e 20 70 61 72 73 65 42 61 73 65 45  eturn parseBaseE
09b0: 78 70 72 65 73 73 69 6f 6e 28 29 3b 0a 09 09 65  xpression();...e
09c0: 6c 73 65 0a 09 09 7b 0a 09 09 09 61 75 74 6f 20  lse...{....auto 
09d0: 6f 70 73 20 3d 20 6f 70 65 72 61 74 6f 72 5f 70  ops = operator_p
09e0: 65 72 66 65 72 65 6e 63 65 73 5b 6c 65 76 65 6c  erferences[level
09f0: 5d 3b 0a 09 09 09 61 75 74 6f 20 65 20 3d 20 70  ];....auto e = p
0a00: 61 72 73 65 45 28 6c 65 76 65 6c 2b 31 29 3b 0a  arseE(level+1);.
0a10: 09 09 73 65 71 3a 0a 09 09 09 77 68 69 6c 65 28  ..seq:....while(
0a20: 20 21 6c 65 78 2e 65 6d 70 74 79 20 29 0a 09 09   !lex.empty )...
0a30: 09 7b 0a 09 09 09 09 61 75 74 6f 20 70 6f 73 20  .{.....auto pos 
0a40: 3d 20 6c 65 78 2e 66 72 6f 6e 74 2e 70 6f 73 3b  = lex.front.pos;
0a50: 0a 09 09 09 09 66 6f 72 65 61 63 68 28 6f 70 3b  .....foreach(op;
0a60: 20 6f 70 73 29 0a 09 09 09 09 09 69 66 28 20 74   ops)......if( t
0a70: 72 79 45 61 74 28 6f 70 29 20 29 0a 09 09 09 09  ryEat(op) ).....
0a80: 09 7b 0a 09 09 09 09 09 09 69 66 28 20 6f 70 20  .{.......if( op 
0a90: 3d 3d 20 22 3d 22 20 29 20 2f 2f 20 72 69 67 68  == "=" ) // righ
0aa0: 74 20 61 73 73 6f 63 0a 09 09 09 09 09 09 09 72  t assoc........r
0ab0: 65 74 75 72 6e 20 6e 65 77 20 41 73 73 69 67 6e  eturn new Assign
0ac0: 45 78 70 72 65 73 73 69 6f 6e 28 65 2e 70 6f 73  Expression(e.pos
0ad0: 2c 20 65 2c 20 70 61 72 73 65 45 28 6c 65 76 65  , e, parseE(leve
0ae0: 6c 29 29 3b 0a 09 09 09 09 09 09 65 6c 73 65 0a  l));.......else.
0af0: 09 09 09 09 09 09 09 65 20 3d 20 6e 65 77 20 46  .......e = new F
0b00: 75 6e 63 61 6c 6c 45 78 70 72 65 73 73 69 6f 6e  uncallExpression
0b10: 28 65 2e 70 6f 73 2c 20 6e 65 77 20 56 61 72 45  (e.pos, new VarE
0b20: 78 70 72 65 73 73 69 6f 6e 28 70 6f 73 2c 20 6f  xpression(pos, o
0b30: 70 29 2c 20 65 2c 20 70 61 72 73 65 45 28 6c 65  p), e, parseE(le
0b40: 76 65 6c 2b 31 29 29 3b 0a 09 09 09 09 09 09 63  vel+1));.......c
0b50: 6f 6e 74 69 6e 75 65 20 73 65 71 3b 0a 09 09 09  ontinue seq;....
0b60: 09 09 7d 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09  ..}.....break;..
0b70: 09 09 7d 0a 09 09 09 72 65 74 75 72 6e 20 65 3b  ..}....return e;
0b80: 0a 09 09 7d 0a 09 7d 0a 0a 09 45 78 70 72 65 73  ...}..}...Expres
0b90: 73 69 6f 6e 20 70 61 72 73 65 42 61 73 65 45 78  sion parseBaseEx
0ba0: 70 72 65 73 73 69 6f 6e 28 29 0a 09 7b 0a 09 09  pression()..{...
0bb0: 69 66 28 20 6c 65 78 2e 65 6d 70 74 79 20 29 0a  if( lex.empty ).
0bc0: 09 09 09 74 68 72 6f 77 20 6e 65 77 20 50 61 72  ...throw new Par
0bd0: 73 65 72 45 78 63 65 70 74 69 6f 6e 28 22 45 4f  serException("EO
0be0: 46 20 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67  F during parsing
0bf0: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29   an expression")
0c00: 3b 0a 09 09 61 75 74 6f 20 70 6f 73 20 3d 20 6c  ;...auto pos = l
0c10: 65 78 2e 66 72 6f 6e 74 2e 70 6f 73 3b 0a 09 09  ex.front.pos;...
0c20: 45 78 70 72 65 73 73 69 6f 6e 20 65 20 3d 20 70  Expression e = p
0c30: 61 72 73 65 42 61 73 65 42 61 73 65 45 78 70 72  arseBaseBaseExpr
0c40: 65 73 73 69 6f 6e 28 29 3b 0a 09 09 77 68 69 6c  ession();...whil
0c50: 65 28 20 74 72 79 45 61 74 28 22 28 22 29 20 29  e( tryEat("(") )
0c60: 20 2f 2f 20 66 75 6e 63 61 6c 6c 0a 09 09 7b 0a   // funcall...{.
0c70: 09 09 09 45 78 70 72 65 73 73 69 6f 6e 5b 5d 20  ...Expression[] 
0c80: 61 72 67 73 3b 0a 09 09 09 77 68 69 6c 65 28 20  args;....while( 
0c90: 21 74 72 79 45 61 74 28 22 29 22 29 20 29 20 7b  !tryEat(")") ) {
0ca0: 0a 09 09 09 09 69 66 28 20 6c 65 78 2e 65 6d 70  .....if( lex.emp
0cb0: 74 79 20 29 20 7b 0a 09 09 09 09 09 61 75 74 6f  ty ) {......auto
0cc0: 20 65 78 20 3d 20 50 61 72 73 65 72 45 78 63 65   ex = ParserExce
0cd0: 70 74 69 6f 6e 2e 63 72 65 61 74 65 28 6c 65 78  ption.create(lex
0ce0: 2c 22 55 6e 65 78 70 65 63 74 65 64 20 45 4f 46  ,"Unexpected EOF
0cf0: 22 29 3b 0a 09 09 09 09 09 74 68 72 6f 77 20 65  ");......throw e
0d00: 78 3b 0a 09 09 09 09 7d 0a 09 09 09 09 61 72 67  x;.....}.....arg
0d10: 73 20 7e 3d 20 70 61 72 73 65 45 28 29 3b 0a 09  s ~= parseE();..
0d20: 09 09 09 69 66 28 20 21 74 72 79 45 61 74 28 22  ...if( !tryEat("
0d30: 2c 22 29 20 29 20 7b 0a 09 09 09 09 09 65 61 74  ,") ) {......eat
0d40: 28 22 29 22 2c 20 22 61 66 74 65 72 20 66 75 6e  (")", "after fun
0d50: 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73  ction parameters
0d60: 22 29 3b 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a  ");......break;.
0d70: 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 09 65 20  ....}....}....e 
0d80: 3d 20 6e 65 77 20 46 75 6e 63 61 6c 6c 45 78 70  = new FuncallExp
0d90: 72 65 73 73 69 6f 6e 28 70 6f 73 2c 20 65 2c 20  ression(pos, e, 
0da0: 61 72 67 73 29 3b 0a 09 09 7d 0a 09 09 72 65 74  args);...}...ret
0db0: 75 72 6e 20 65 3b 0a 09 7d 0a 0a 09 45 78 70 72  urn e;..}...Expr
0dc0: 65 73 73 69 6f 6e 20 70 61 72 73 65 42 61 73 65  ession parseBase
0dd0: 42 61 73 65 45 78 70 72 65 73 73 69 6f 6e 28 29  BaseExpression()
0de0: 0a 09 7b 0a 09 09 69 66 28 20 6c 65 78 2e 65 6d  ..{...if( lex.em
0df0: 70 74 79 20 29 0a 09 09 09 74 68 72 6f 77 20 6e  pty )....throw n
0e00: 65 77 20 50 61 72 73 65 72 45 78 63 65 70 74 69  ew ParserExcepti
0e10: 6f 6e 28 22 45 4f 46 20 64 75 72 69 6e 67 20 70  on("EOF during p
0e20: 61 72 73 69 6e 67 20 61 6e 20 65 78 70 72 65 73  arsing an expres
0e30: 73 69 6f 6e 22 29 3b 0a 09 09 61 75 74 6f 20 70  sion");...auto p
0e40: 6f 73 20 3d 20 6c 65 78 2e 66 72 6f 6e 74 2e 70  os = lex.front.p
0e50: 6f 73 3b 0a 0a 09 09 69 66 28 20 6c 65 78 2e 66  os;....if( lex.f
0e60: 72 6f 6e 74 2e 71 75 6f 74 65 64 20 29 0a 09 09  ront.quoted )...
0e70: 7b 0a 09 09 09 73 63 6f 70 65 28 65 78 69 74 29  {....scope(exit)
0e80: 20 6c 65 78 2e 70 6f 70 46 72 6f 6e 74 3b 0a 09   lex.popFront;..
0e90: 09 09 72 65 74 75 72 6e 20 6e 65 77 20 53 74 72  ..return new Str
0ea0: 4c 69 74 65 72 61 6c 45 78 70 72 65 73 73 69 6f  LiteralExpressio
0eb0: 6e 28 70 6f 73 2c 20 6c 65 78 2e 66 72 6f 6e 74  n(pos, lex.front
0ec0: 2e 73 74 72 29 3b 0a 09 09 7d 0a 09 09 69 66 28  .str);...}...if(
0ed0: 20 69 73 4e 75 6d 62 65 72 28 6c 65 78 2e 66 72   isNumber(lex.fr
0ee0: 6f 6e 74 2e 73 74 72 29 20 29 20 2f 2f 20 69 73  ont.str) ) // is
0ef0: 5f 6e 75 6d 62 65 72 0a 09 09 7b 0a 09 09 09 73  _number...{....s
0f00: 63 6f 70 65 28 65 78 69 74 29 20 6c 65 78 2e 70  cope(exit) lex.p
0f10: 6f 70 46 72 6f 6e 74 3b 0a 09 09 09 72 65 74 75  opFront;....retu
0f20: 72 6e 20 6e 65 77 20 49 6e 74 4c 69 74 65 72 61  rn new IntLitera
0f30: 6c 45 78 70 72 65 73 73 69 6f 6e 28 70 6f 73 2c  lExpression(pos,
0f40: 20 42 69 67 49 6e 74 28 63 61 73 74 28 73 74 72   BigInt(cast(str
0f50: 69 6e 67 29 6c 65 78 2e 66 72 6f 6e 74 2e 73 74  ing)lex.front.st
0f60: 72 29 29 3b 0a 09 09 7d 0a 09 09 69 66 28 20 74  r));...}...if( t
0f70: 72 79 45 61 74 28 22 28 22 29 20 29 0a 09 09 7b  ryEat("(") )...{
0f80: 0a 09 09 09 61 75 74 6f 20 65 20 3d 20 70 61 72  ....auto e = par
0f90: 73 65 45 28 29 3b 0a 09 09 09 65 61 74 28 22 29  seE();....eat(")
0fa0: 22 2c 20 22 61 66 74 65 72 20 70 61 72 65 6e 74  ", "after parent
0fb0: 68 65 73 69 7a 65 64 20 65 78 70 72 65 73 73 69  hesized expressi
0fc0: 6f 6e 22 29 3b 0a 09 09 09 72 65 74 75 72 6e 20  on");....return 
0fd0: 65 3b 0a 09 09 7d 0a 09 09 69 66 28 20 74 72 79  e;...}...if( try
0fe0: 45 61 74 28 22 69 66 22 29 20 29 0a 09 09 7b 0a  Eat("if") )...{.
0ff0: 09 09 09 65 61 74 28 22 28 22 2c 20 22 61 66 74  ...eat("(", "aft
1000: 65 72 20 69 66 22 29 3b 0a 09 09 09 61 75 74 6f  er if");....auto
1010: 20 63 6f 6e 64 20 3d 20 70 61 72 73 65 45 28 29   cond = parseE()
1020: 3b 0a 09 09 09 65 61 74 28 22 29 22 2c 20 22 61  ;....eat(")", "a
1030: 66 74 65 72 20 69 66 20 63 6f 6e 64 69 74 69 6f  fter if conditio
1040: 6e 22 29 3b 0a 09 09 09 61 75 74 6f 20 74 68 65  n");....auto the
1050: 6e 50 6f 73 20 3d 20 6c 65 78 2e 66 72 6f 6e 74  nPos = lex.front
1060: 2e 70 6f 73 3b 0a 09 09 09 65 61 74 28 22 7b 22  .pos;....eat("{"
1070: 2c 20 22 61 66 74 65 72 20 69 66 20 63 6f 6e 64  , "after if cond
1080: 69 74 69 6f 6e 22 29 3b 0a 09 09 09 53 74 61 74  ition");....Stat
1090: 65 6d 65 6e 74 5b 5d 20 74 68 20 3d 20 70 61 72  ement[] th = par
10a0: 73 65 53 74 61 74 65 6d 65 6e 74 73 28 29 3b 0a  seStatements();.
10b0: 09 09 09 65 61 74 28 22 7d 22 2c 20 22 61 66 74  ...eat("}", "aft
10c0: 65 72 20 69 66 2d 74 68 65 6e 20 62 6f 64 79 22  er if-then body"
10d0: 29 3b 0a 09 09 09 53 74 61 74 65 6d 65 6e 74 5b  );....Statement[
10e0: 5d 20 65 6c 3b 0a 09 09 09 61 75 74 6f 20 65 6c  ] el;....auto el
10f0: 73 65 50 6f 73 20 3d 20 6c 65 78 2e 66 72 6f 6e  sePos = lex.fron
1100: 74 2e 70 6f 73 3b 0a 09 09 09 69 66 28 20 74 72  t.pos;....if( tr
1110: 79 45 61 74 28 22 65 6c 73 65 22 29 20 29 20 7b  yEat("else") ) {
1120: 0a 09 09 09 09 65 61 74 28 22 7b 22 2c 20 22 61  .....eat("{", "a
1130: 66 74 65 72 20 65 6c 73 65 22 29 3b 0a 09 09 09  fter else");....
1140: 09 65 6c 20 3d 20 70 61 72 73 65 53 74 61 74 65  .el = parseState
1150: 6d 65 6e 74 73 28 29 3b 0a 09 09 09 09 65 61 74  ments();.....eat
1160: 28 22 7d 22 2c 20 22 61 66 74 65 72 20 65 6c 73  ("}", "after els
1170: 65 20 62 6f 64 79 22 29 3b 0a 09 09 09 7d 0a 09  e body");....}..
1180: 09 09 72 65 74 75 72 6e 20 6e 65 77 20 46 75 6e  ..return new Fun
1190: 63 61 6c 6c 45 78 70 72 65 73 73 69 6f 6e 28 70  callExpression(p
11a0: 6f 73 2c 20 0a 09 09 09 09 6e 65 77 20 56 61 72  os, .....new Var
11b0: 45 78 70 72 65 73 73 69 6f 6e 28 70 6f 73 2c 20  Expression(pos, 
11c0: 22 69 66 22 29 2c 0a 09 09 09 09 63 6f 6e 64 2c  "if"),.....cond,
11d0: 0a 09 09 09 09 6e 65 77 20 46 75 6e 4c 69 74 65  .....new FunLite
11e0: 72 61 6c 45 78 70 72 65 73 73 69 6f 6e 28 74 68  ralExpression(th
11f0: 65 6e 50 6f 73 2c 20 5b 5d 2c 20 74 68 29 2c 0a  enPos, [], th),.
1200: 09 09 09 09 6e 65 77 20 46 75 6e 4c 69 74 65 72  ....new FunLiter
1210: 61 6c 45 78 70 72 65 73 73 69 6f 6e 28 65 6c 73  alExpression(els
1220: 65 50 6f 73 2c 20 5b 5d 2c 20 65 6c 29 0a 09 09  ePos, [], el)...
1230: 09 29 3b 0a 09 09 7d 0a 0a 09 09 69 66 28 20 74  .);...}....if( t
1240: 72 79 45 61 74 28 22 66 75 6e 22 29 20 29 0a 09  ryEat("fun") )..
1250: 09 7b 0a 09 09 09 65 61 74 28 22 28 22 2c 20 22  .{....eat("(", "
1260: 61 66 74 65 72 20 66 75 6e 22 29 3b 0a 09 09 09  after fun");....
1270: 73 74 72 69 6e 67 5b 5d 20 70 61 72 61 6d 73 3b  string[] params;
1280: 0a 09 09 09 77 68 69 6c 65 28 21 74 72 79 45 61  ....while(!tryEa
1290: 74 28 22 29 22 29 29 0a 09 09 09 7b 0a 09 09 09  t(")"))....{....
12a0: 09 69 66 28 20 6c 65 78 2e 65 6d 70 74 79 20 29  .if( lex.empty )
12b0: 20 7b 0a 09 09 09 09 09 61 75 74 6f 20 65 20 3d   {......auto e =
12c0: 20 50 61 72 73 65 72 45 78 63 65 70 74 69 6f 6e   ParserException
12d0: 2e 63 72 65 61 74 65 28 6c 65 78 2c 22 55 6e 65  .create(lex,"Une
12e0: 78 70 65 63 74 65 64 20 45 4f 46 22 29 3b 0a 09  xpected EOF");..
12f0: 09 09 09 09 74 68 72 6f 77 20 65 3b 0a 09 09 09  ....throw e;....
1300: 09 7d 0a 09 09 09 09 69 66 28 20 6c 65 78 2e 66  .}.....if( lex.f
1310: 72 6f 6e 74 2e 71 75 6f 74 65 64 20 29 20 7b 0a  ront.quoted ) {.
1320: 09 09 09 09 09 61 75 74 6f 20 65 20 3d 20 50 61  .....auto e = Pa
1330: 72 73 65 72 45 78 63 65 70 74 69 6f 6e 2e 63 72  rserException.cr
1340: 65 61 74 65 28 6c 65 78 2c 22 49 64 65 6e 74 69  eate(lex,"Identi
1350: 66 69 65 72 20 45 78 70 65 63 74 65 64 20 66 6f  fier Expected fo
1360: 72 20 70 61 72 61 6d 65 74 65 72 73 22 29 3b 0a  r parameters");.
1370: 09 09 09 09 09 74 68 72 6f 77 20 65 3b 0a 09 09  .....throw e;...
1380: 09 09 7d 0a 09 09 09 09 70 61 72 61 6d 73 20 7e  ..}.....params ~
1390: 3d 20 6c 65 78 2e 66 72 6f 6e 74 2e 73 74 72 3b  = lex.front.str;
13a0: 0a 09 09 09 09 6c 65 78 2e 70 6f 70 46 72 6f 6e  .....lex.popFron
13b0: 74 3b 0a 09 09 09 09 69 66 28 20 21 74 72 79 45  t;.....if( !tryE
13c0: 61 74 28 22 2c 22 29 20 29 20 7b 0a 09 09 09 09  at(",") ) {.....
13d0: 09 65 61 74 28 22 29 22 2c 20 22 61 66 74 65 72  .eat(")", "after
13e0: 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
13f0: 74 65 72 73 22 29 3b 0a 09 09 09 09 09 62 72 65  ters");......bre
1400: 61 6b 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09  ak;.....}....}..
1410: 09 09 65 61 74 28 22 7b 22 2c 20 22 61 66 74 65  ..eat("{", "afte
1420: 72 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d  r function param
1430: 65 74 65 72 73 22 29 3b 0a 09 09 09 53 74 61 74  eters");....Stat
1440: 65 6d 65 6e 74 5b 5d 20 66 75 6e 62 6f 64 79 3b  ement[] funbody;
1450: 0a 09 09 09 77 68 69 6c 65 28 21 74 72 79 45 61  ....while(!tryEa
1460: 74 28 22 7d 22 29 29 20 7b 0a 09 09 09 09 69 66  t("}")) {.....if
1470: 28 20 6c 65 78 2e 65 6d 70 74 79 20 29 20 7b 0a  ( lex.empty ) {.
1480: 09 09 09 09 09 61 75 74 6f 20 65 20 3d 20 50 61  .....auto e = Pa
1490: 72 73 65 72 45 78 63 65 70 74 69 6f 6e 2e 63 72  rserException.cr
14a0: 65 61 74 65 28 6c 65 78 2c 22 55 6e 65 78 70 65  eate(lex,"Unexpe
14b0: 63 74 65 64 20 45 4f 46 22 29 3b 0a 09 09 09 09  cted EOF");.....
14c0: 09 74 68 72 6f 77 20 65 3b 0a 09 09 09 09 7d 0a  .throw e;.....}.
14d0: 09 09 09 09 66 75 6e 62 6f 64 79 20 7e 3d 20 70  ....funbody ~= p
14e0: 61 72 73 65 53 74 61 74 65 6d 65 6e 74 28 29 3b  arseStatement();
14f0: 0a 09 09 09 7d 0a 09 09 09 72 65 74 75 72 6e 20  ....}....return 
1500: 6e 65 77 20 46 75 6e 4c 69 74 65 72 61 6c 45 78  new FunLiteralEx
1510: 70 72 65 73 73 69 6f 6e 28 70 6f 73 2c 20 70 61  pression(pos, pa
1520: 72 61 6d 73 2c 20 66 75 6e 62 6f 64 79 29 3b 0a  rams, funbody);.
1530: 09 09 7d 0a 09 09 73 63 6f 70 65 28 65 78 69 74  ..}...scope(exit
1540: 29 20 6c 65 78 2e 70 6f 70 46 72 6f 6e 74 3b 0a  ) lex.popFront;.
1550: 09 09 72 65 74 75 72 6e 20 6e 65 77 20 56 61 72  ..return new Var
1560: 45 78 70 72 65 73 73 69 6f 6e 28 70 6f 73 2c 20  Expression(pos, 
1570: 6c 65 78 2e 66 72 6f 6e 74 2e 73 74 72 29 3b 0a  lex.front.str);.
1580: 09 7d 0a 0a 70 72 69 76 61 74 65 3a 0a 09 4c 65  .}..private:..Le
1590: 78 65 72 20 6c 65 78 3b 0a 0a 09 76 6f 69 64 20  xer lex;...void 
15a0: 65 61 74 28 73 74 72 69 6e 67 20 6b 77 64 2c 20  eat(string kwd, 
15b0: 6c 61 7a 79 20 73 74 72 69 6e 67 20 6d 73 67 29  lazy string msg)
15c0: 0a 09 7b 0a 09 09 69 66 28 20 21 74 72 79 45 61  ..{...if( !tryEa
15d0: 74 28 6b 77 64 29 20 29 0a 09 09 7b 0a 09 09 09  t(kwd) )...{....
15e0: 61 75 74 6f 20 65 20 3d 20 50 61 72 73 65 72 45  auto e = ParserE
15f0: 78 63 65 70 74 69 6f 6e 2e 63 72 65 61 74 65 28  xception.create(
1600: 6c 65 78 2c 20 22 27 22 7e 6b 77 64 7e 22 27 20  lex, "'"~kwd~"' 
1610: 69 73 20 65 78 70 65 63 74 65 64 20 22 7e 6d 73  is expected "~ms
1620: 67 7e 22 20 62 75 74 20 27 22 0a 09 09 09 09 7e  g~" but '".....~
1630: 28 6c 65 78 2e 65 6d 70 74 79 20 3f 20 22 45 4f  (lex.empty ? "EO
1640: 46 22 20 3a 20 6c 65 78 2e 66 72 6f 6e 74 2e 73  F" : lex.front.s
1650: 74 72 29 7e 22 27 20 6f 63 63 75 72 65 64 22 29  tr)~"' occured")
1660: 3b 0a 09 09 09 74 68 72 6f 77 20 65 3b 0a 09 09  ;....throw e;...
1670: 7d 0a 09 7d 0a 0a 09 62 6f 6f 6c 20 74 72 79 45  }..}...bool tryE
1680: 61 74 28 73 74 72 69 6e 67 20 6b 77 64 29 0a 09  at(string kwd)..
1690: 7b 0a 09 09 69 66 28 20 6c 65 78 2e 65 6d 70 74  {...if( lex.empt
16a0: 79 20 7c 7c 20 6c 65 78 2e 66 72 6f 6e 74 2e 71  y || lex.front.q
16b0: 75 6f 74 65 64 20 7c 7c 20 6c 65 78 2e 66 72 6f  uoted || lex.fro
16c0: 6e 74 2e 73 74 72 21 3d 6b 77 64 20 29 0a 09 09  nt.str!=kwd )...
16d0: 09 72 65 74 75 72 6e 20 66 61 6c 73 65 3b 0a 09  .return false;..
16e0: 09 6c 65 78 2e 70 6f 70 46 72 6f 6e 74 3b 0a 09  .lex.popFront;..
16f0: 09 72 65 74 75 72 6e 20 74 72 75 65 3b 0a 09 7d  .return true;..}
1700: 0a 0a 09 62 6f 6f 6c 20 69 73 4e 75 6d 62 65 72  ...bool isNumber
1710: 28 73 74 72 69 6e 67 20 73 29 0a 09 7b 0a 09 09  (string s)..{...
1720: 72 65 74 75 72 6e 20 66 69 6e 64 21 28 60 61 3c  return find!(`a<
1730: 27 30 27 7c 7c 27 39 27 3c 61 60 29 28 73 29 2e  '0'||'9'<a`)(s).
1740: 65 6d 70 74 79 3b 0a 09 7d 0a 7d 0a 0a 75 6e 69  empty;..}.}..uni
1750: 74 74 65 73 74 0a 7b 0a 09 61 75 74 6f 20 70 20  ttest.{..auto p 
1760: 3d 20 70 61 72 73 65 72 46 72 6f 6d 53 74 72 69  = parserFromStri
1770: 6e 67 28 60 0a 09 09 76 61 72 20 78 20 3d 20 31  ng(`...var x = 1
1780: 30 30 3b 0a 09 09 76 61 72 20 79 20 3d 20 32 30  00;...var y = 20
1790: 30 3b 0a 09 60 29 3b 0a 09 50 72 6f 67 72 61 6d  0;..`);..Program
17a0: 20 70 72 6f 67 20 3d 20 70 2e 70 61 72 73 65 50   prog = p.parseP
17b0: 72 6f 67 72 61 6d 28 29 3b 0a 09 61 73 73 65 72  rogram();..asser
17c0: 74 28 20 70 72 6f 67 2e 6c 65 6e 67 74 68 20 3d  t( prog.length =
17d0: 3d 20 32 20 29 3b 0a 09 61 75 74 6f 20 70 30 20  = 2 );..auto p0 
17e0: 3d 20 63 61 73 74 28 44 65 63 6c 53 74 61 74 65  = cast(DeclState
17f0: 6d 65 6e 74 29 70 72 6f 67 5b 30 5d 3b 0a 09 61  ment)prog[0];..a
1800: 75 74 6f 20 70 31 20 3d 20 63 61 73 74 28 44 65  uto p1 = cast(De
1810: 63 6c 53 74 61 74 65 6d 65 6e 74 29 70 72 6f 67  clStatement)prog
1820: 5b 31 5d 3b 0a 09 61 73 73 65 72 74 28 20 70 30  [1];..assert( p0
1830: 2e 76 61 72 20 3d 3d 20 22 78 22 20 29 3b 0a 09  .var == "x" );..
1840: 61 73 73 65 72 74 28 20 70 31 2e 76 61 72 20 3d  assert( p1.var =
1850: 3d 20 22 79 22 20 29 3b 0a 09 61 73 73 65 72 74  = "y" );..assert
1860: 28 20 28 63 61 73 74 28 49 6e 74 4c 69 74 65 72  ( (cast(IntLiter
1870: 61 6c 45 78 70 72 65 73 73 69 6f 6e 29 70 30 2e  alExpression)p0.
1880: 65 78 70 72 29 2e 64 61 74 61 20 3d 3d 20 31 30  expr).data == 10
1890: 30 20 29 3b 0a 09 61 73 73 65 72 74 28 20 28 63  0 );..assert( (c
18a0: 61 73 74 28 49 6e 74 4c 69 74 65 72 61 6c 45 78  ast(IntLiteralEx
18b0: 70 72 65 73 73 69 6f 6e 29 70 31 2e 65 78 70 72  pression)p1.expr
18c0: 29 2e 64 61 74 61 20 3d 3d 20 32 30 30 20 29 3b  ).data == 200 );
18d0: 0a 7d 0a 0a 75 6e 69 74 74 65 73 74 0a 7b 0a 09  .}..unittest.{..
18e0: 61 75 74 6f 20 70 20 3d 20 70 61 72 73 65 72 46  auto p = parserF
18f0: 72 6f 6d 53 74 72 69 6e 67 28 60 0a 09 09 76 61  romString(`...va
1900: 72 20 7a 7a 7a 20 3d 20 31 30 30 3b 20 23 20 63  r zzz = 100; # c
1910: 6f 6d 6d 65 6e 74 0a 09 09 7a 7a 7a 20 3d 20 7a  omment...zzz = z
1920: 7a 7a 20 2b 20 7a 7a 7a 20 2a 20 22 66 6f 5c 6e  zz + zzz * "fo\n
1930: 6f 22 3b 20 23 20 63 6f 6d 6d 65 6e 74 0a 09 09  o"; # comment...
1940: 09 34 32 3b 0a 09 60 29 3b 0a 09 0a 09 61 75 74  .42;..`);....aut
1950: 6f 20 73 30 20 3d 20 6e 65 77 20 44 65 63 6c 53  o s0 = new DeclS
1960: 74 61 74 65 6d 65 6e 74 28 6e 75 6c 6c 2c 20 22  tatement(null, "
1970: 7a 7a 7a 22 2c 20 6e 65 77 20 49 6e 74 4c 69 74  zzz", new IntLit
1980: 65 72 61 6c 45 78 70 72 65 73 73 69 6f 6e 28 6e  eralExpression(n
1990: 75 6c 6c 2c 20 42 69 67 49 6e 74 28 31 30 30 29  ull, BigInt(100)
19a0: 29 29 3b 0a 09 61 75 74 6f 20 73 31 20 3d 20 6e  ));..auto s1 = n
19b0: 65 77 20 45 78 70 72 53 74 61 74 65 6d 65 6e 74  ew ExprStatement
19c0: 28 6e 75 6c 6c 2c 20 6e 65 77 20 41 73 73 69 67  (null, new Assig
19d0: 6e 45 78 70 72 65 73 73 69 6f 6e 28 6e 75 6c 6c  nExpression(null
19e0: 2c 0a 09 09 6e 65 77 20 56 61 72 45 78 70 72 65  ,...new VarExpre
19f0: 73 73 69 6f 6e 28 6e 75 6c 6c 2c 20 22 7a 7a 7a  ssion(null, "zzz
1a00: 22 29 2c 0a 09 09 6e 65 77 20 46 75 6e 63 61 6c  "),...new Funcal
1a10: 6c 45 78 70 72 65 73 73 69 6f 6e 28 6e 75 6c 6c  lExpression(null
1a20: 2c 20 6e 65 77 20 56 61 72 45 78 70 72 65 73 73  , new VarExpress
1a30: 69 6f 6e 28 6e 75 6c 6c 2c 22 2b 22 29 2c 0a 09  ion(null,"+"),..
1a40: 09 09 6e 65 77 20 56 61 72 45 78 70 72 65 73 73  ..new VarExpress
1a50: 69 6f 6e 28 6e 75 6c 6c 2c 20 22 7a 7a 7a 22 29  ion(null, "zzz")
1a60: 2c 0a 09 09 09 6e 65 77 20 46 75 6e 63 61 6c 6c  ,....new Funcall
1a70: 45 78 70 72 65 73 73 69 6f 6e 28 6e 75 6c 6c 2c  Expression(null,
1a80: 20 6e 65 77 20 56 61 72 45 78 70 72 65 73 73 69   new VarExpressi
1a90: 6f 6e 28 6e 75 6c 6c 2c 22 2a 22 29 2c 0a 09 09  on(null,"*"),...
1aa0: 09 09 6e 65 77 20 56 61 72 45 78 70 72 65 73 73  ..new VarExpress
1ab0: 69 6f 6e 28 6e 75 6c 6c 2c 20 22 7a 7a 7a 22 29  ion(null, "zzz")
1ac0: 2c 0a 09 09 09 09 6e 65 77 20 53 74 72 4c 69 74  ,.....new StrLit
1ad0: 65 72 61 6c 45 78 70 72 65 73 73 69 6f 6e 28 6e  eralExpression(n
1ae0: 75 6c 6c 2c 20 22 66 6f 5c 5c 6e 6f 22 29 0a 09  ull, "fo\\no")..
1af0: 09 09 09 29 29 29 29 3b 0a 09 61 75 74 6f 20 73  ...))));..auto s
1b00: 32 20 3d 20 6e 65 77 20 45 78 70 72 53 74 61 74  2 = new ExprStat
1b10: 65 6d 65 6e 74 28 6e 75 6c 6c 2c 20 6e 65 77 20  ement(null, new 
1b20: 49 6e 74 4c 69 74 65 72 61 6c 45 78 70 72 65 73  IntLiteralExpres
1b30: 73 69 6f 6e 28 6e 75 6c 6c 2c 20 42 69 67 49 6e  sion(null, BigIn
1b40: 74 28 34 32 29 29 29 3b 0a 09 0a 09 50 72 6f 67  t(42)));....Prog
1b50: 72 61 6d 20 70 72 6f 67 20 3d 20 70 2e 70 61 72  ram prog = p.par
1b60: 73 65 50 72 6f 67 72 61 6d 28 29 3b 0a 09 61 73  seProgram();..as
1b70: 73 65 72 74 28 20 70 72 6f 67 2e 6c 65 6e 67 74  sert( prog.lengt
1b80: 68 20 3d 3d 20 33 20 29 3b 0a 09 61 73 73 65 72  h == 3 );..asser
1b90: 74 28 20 70 72 6f 67 5b 30 5d 20 3d 3d 20 73 30  t( prog[0] == s0
1ba0: 20 29 3b 0a 09 61 73 73 65 72 74 28 20 70 72 6f   );..assert( pro
1bb0: 67 5b 31 5d 20 3d 3d 20 73 31 20 29 3b 0a 09 61  g[1] == s1 );..a
1bc0: 73 73 65 72 74 28 20 70 72 6f 67 5b 32 5d 20 3d  ssert( prog[2] =
1bd0: 3d 20 73 32 20 29 3b 0a 7d 0a 0a 75 6e 69 74 74  = s2 );.}..unitt
1be0: 65 73 74 0a 7b 0a 09 61 75 74 6f 20 70 20 3d 20  est.{..auto p = 
1bf0: 70 61 72 73 65 72 46 72 6f 6d 53 74 72 69 6e 67  parserFromString
1c00: 28 60 0a 09 09 76 61 72 20 66 20 3d 20 66 75 6e  (`...var f = fun
1c10: 28 78 2c 79 29 7b 78 2b 79 3b 7d 3b 0a 09 09 66  (x,y){x+y;};...f
1c20: 28 31 2c 66 75 6e 28 61 62 63 29 7b 7d 28 34 29  (1,fun(abc){}(4)
1c30: 29 3b 0a 09 60 29 3b 0a 09 50 72 6f 67 72 61 6d  );..`);..Program
1c40: 20 70 72 6f 67 20 3d 20 70 2e 70 61 72 73 65 50   prog = p.parseP
1c50: 72 6f 67 72 61 6d 28 29 3b 0a 09 61 73 73 65 72  rogram();..asser
1c60: 74 28 20 70 72 6f 67 2e 6c 65 6e 67 74 68 20 3d  t( prog.length =
1c70: 3d 20 32 20 29 3b 0a 09 61 73 73 65 72 74 28 20  = 2 );..assert( 
1c80: 70 72 6f 67 5b 30 5d 20 3d 3d 20 6e 65 77 20 44  prog[0] == new D
1c90: 65 63 6c 53 74 61 74 65 6d 65 6e 74 28 6e 75 6c  eclStatement(nul
1ca0: 6c 2c 20 22 66 22 2c 20 6e 65 77 20 46 75 6e 4c  l, "f", new FunL
1cb0: 69 74 65 72 61 6c 45 78 70 72 65 73 73 69 6f 6e  iteralExpression
1cc0: 28 6e 75 6c 6c 2c 0a 09 09 5b 22 78 22 2c 22 79  (null,...["x","y
1cd0: 22 5d 2c 20 5b 6e 65 77 20 45 78 70 72 53 74 61  "], [new ExprSta
1ce0: 74 65 6d 65 6e 74 28 6e 75 6c 6c 2c 0a 09 09 09  tement(null,....
1cf0: 6e 65 77 20 46 75 6e 63 61 6c 6c 45 78 70 72 65  new FuncallExpre
1d00: 73 73 69 6f 6e 28 6e 75 6c 6c 2c 20 6e 65 77 20  ssion(null, new 
1d10: 56 61 72 45 78 70 72 65 73 73 69 6f 6e 28 6e 75  VarExpression(nu
1d20: 6c 6c 2c 20 22 2b 22 29 2c 0a 09 09 09 6e 65 77  ll, "+"),....new
1d30: 20 56 61 72 45 78 70 72 65 73 73 69 6f 6e 28 6e   VarExpression(n
1d40: 75 6c 6c 2c 20 22 78 22 29 2c 20 6e 65 77 20 56  ull, "x"), new V
1d50: 61 72 45 78 70 72 65 73 73 69 6f 6e 28 6e 75 6c  arExpression(nul
1d60: 6c 2c 20 22 79 22 29 29 29 5d 0a 09 09 09 29 29  l, "y")))]....))
1d70: 29 3b 0a 09 61 73 73 65 72 74 28 20 70 72 6f 67  );..assert( prog
1d80: 5b 31 5d 20 3d 3d 20 6e 65 77 20 45 78 70 72 53  [1] == new ExprS
1d90: 74 61 74 65 6d 65 6e 74 28 6e 75 6c 6c 2c 20 6e  tatement(null, n
1da0: 65 77 20 46 75 6e 63 61 6c 6c 45 78 70 72 65 73  ew FuncallExpres
1db0: 73 69 6f 6e 28 6e 75 6c 6c 2c 0a 09 09 6e 65 77  sion(null,...new
1dc0: 20 56 61 72 45 78 70 72 65 73 73 69 6f 6e 28 6e   VarExpression(n
1dd0: 75 6c 6c 2c 20 22 66 22 29 2c 0a 09 09 6e 65 77  ull, "f"),...new
1de0: 20 49 6e 74 4c 69 74 65 72 61 6c 45 78 70 72 65   IntLiteralExpre
1df0: 73 73 69 6f 6e 28 6e 75 6c 6c 2c 20 42 69 67 49  ssion(null, BigI
1e00: 6e 74 28 31 29 29 2c 0a 09 09 6e 65 77 20 46 75  nt(1)),...new Fu
1e10: 6e 63 61 6c 6c 45 78 70 72 65 73 73 69 6f 6e 28  ncallExpression(
1e20: 6e 75 6c 6c 2c 0a 09 09 09 6e 65 77 20 46 75 6e  null,....new Fun
1e30: 4c 69 74 65 72 61 6c 45 78 70 72 65 73 73 69 6f  LiteralExpressio
1e40: 6e 28 6e 75 6c 6c 2c 20 5b 22 61 62 63 22 5d 2c  n(null, ["abc"],
1e50: 20 5b 0a 09 09 09 5d 29 2c 0a 09 09 09 6e 65 77   [....]),....new
1e60: 20 49 6e 74 4c 69 74 65 72 61 6c 45 78 70 72 65   IntLiteralExpre
1e70: 73 73 69 6f 6e 28 6e 75 6c 6c 2c 20 42 69 67 49  ssion(null, BigI
1e80: 6e 74 28 34 29 29 0a 09 09 09 29 29 29 29 3b 0a  nt(4))....))));.
1e90: 7d 0a 0a 75 6e 69 74 74 65 73 74 0a 7b 0a 09 61  }..unittest.{..a
1ea0: 75 74 6f 20 70 20 3d 20 70 61 72 73 65 72 46 72  uto p = parserFr
1eb0: 6f 6d 53 74 72 69 6e 67 28 60 76 61 72 20 78 20  omString(`var x 
1ec0: 3d 20 31 3b 20 76 61 72 20 66 20 3d 20 66 75 6e  = 1; var f = fun
1ed0: 28 29 7b 78 3d 78 2b 31 3b 7d 3b 20 66 28 29 3b  (){x=x+1;}; f();
1ee0: 20 66 28 29 3b 20 78 3b 60 29 3b 0a 09 50 72 6f   f(); x;`);..Pro
1ef0: 67 72 61 6d 20 70 72 6f 67 20 3d 20 70 2e 70 61  gram prog = p.pa
1f00: 72 73 65 50 72 6f 67 72 61 6d 28 29 3b 0a 7d 0a  rseProgram();.}.
1f10: 0a 75 6e 69 74 74 65 73 74 0a 7b 0a 09 61 75 74  .unittest.{..aut
1f20: 6f 20 70 20 3d 20 70 61 72 73 65 72 46 72 6f 6d  o p = parserFrom
1f30: 53 74 72 69 6e 67 28 60 69 66 28 78 3c 32 29 7b  String(`if(x<2){
1f40: 31 3b 7d 65 6c 73 65 7b 78 3b 7d 3b 60 29 3b 0a  1;}else{x;};`);.
1f50: 09 50 72 6f 67 72 61 6d 20 70 72 6f 67 20 3d 20  .Program prog = 
1f60: 70 2e 70 61 72 73 65 50 72 6f 67 72 61 6d 28 29  p.parseProgram()
1f70: 3b 0a 09 61 73 73 65 72 74 28 20 70 72 6f 67 5b  ;..assert( prog[
1f80: 30 5d 20 3d 3d 20 6e 65 77 20 45 78 70 72 53 74  0] == new ExprSt
1f90: 61 74 65 6d 65 6e 74 28 6e 75 6c 6c 2c 20 6e 65  atement(null, ne
1fa0: 77 20 46 75 6e 63 61 6c 6c 45 78 70 72 65 73 73  w FuncallExpress
1fb0: 69 6f 6e 28 6e 75 6c 6c 2c 0a 09 09 6e 65 77 20  ion(null,...new 
1fc0: 56 61 72 45 78 70 72 65 73 73 69 6f 6e 28 6e 75  VarExpression(nu
1fd0: 6c 6c 2c 20 22 69 66 22 29 2c 0a 09 09 6e 65 77  ll, "if"),...new
1fe0: 20 46 75 6e 63 61 6c 6c 45 78 70 72 65 73 73 69   FuncallExpressi
1ff0: 6f 6e 28 6e 75 6c 6c 2c 20 6e 65 77 20 56 61 72  on(null, new Var
2000: 45 78 70 72 65 73 73 69 6f 6e 28 6e 75 6c 6c 2c  Expression(null,
2010: 22 3c 22 29 2c 20 6e 65 77 20 56 61 72 45 78 70  "<"), new VarExp
2020: 72 65 73 73 69 6f 6e 28 6e 75 6c 6c 2c 22 78 22  ression(null,"x"
2030: 29 2c 0a 09 09 09 6e 65 77 20 49 6e 74 4c 69 74  ),....new IntLit
2040: 65 72 61 6c 45 78 70 72 65 73 73 69 6f 6e 28 6e  eralExpression(n
2050: 75 6c 6c 2c 20 42 69 67 49 6e 74 28 32 29 29 29  ull, BigInt(2)))
2060: 2c 0a 09 09 6e 65 77 20 46 75 6e 4c 69 74 65 72  ,...new FunLiter
2070: 61 6c 45 78 70 72 65 73 73 69 6f 6e 28 6e 75 6c  alExpression(nul
2080: 6c 2c 20 5b 5d 2c 20 5b 6e 65 77 20 45 78 70 72  l, [], [new Expr
2090: 53 74 61 74 65 6d 65 6e 74 28 6e 75 6c 6c 2c 20  Statement(null, 
20a0: 6e 65 77 20 49 6e 74 4c 69 74 65 72 61 6c 45 78  new IntLiteralEx
20b0: 70 72 65 73 73 69 6f 6e 28 6e 75 6c 6c 2c 20 42  pression(null, B
20c0: 69 67 49 6e 74 28 31 29 29 29 5d 29 2c 0a 09 09  igInt(1)))]),...
20d0: 6e 65 77 20 46 75 6e 4c 69 74 65 72 61 6c 45 78  new FunLiteralEx
20e0: 70 72 65 73 73 69 6f 6e 28 6e 75 6c 6c 2c 20 5b  pression(null, [
20f0: 5d 2c 20 5b 6e 65 77 20 45 78 70 72 53 74 61 74  ], [new ExprStat
2100: 65 6d 65 6e 74 28 6e 75 6c 6c 2c 20 6e 65 77 20  ement(null, new 
2110: 56 61 72 45 78 70 72 65 73 73 69 6f 6e 28 6e 75  VarExpression(nu
2120: 6c 6c 2c 20 22 78 22 29 29 5d 29 0a 09 09 29 29  ll, "x"))])...))
2130: 29 3b 09 09 0a 7d 0a                             );...}.