Hex Artifact Content
Not logged in

Artifact 701f5b1b899607f242de31801cedde45d2a2eda1:


0000: ef bb bf 2f 2a 2a 0d 0a 20 2a 20 41 75 74 68 6f  .../**.. * Autho
0010: 72 73 3a 20 6b 2e 69 6e 61 62 61 0d 0a 20 2a 20  rs: k.inaba.. * 
0020: 4c 69 63 65 6e 73 65 3a 20 4e 59 53 4c 20 30 2e  License: NYSL 0.
0030: 39 39 38 32 20 68 74 74 70 3a 2f 2f 77 77 77 2e  9982 http://www.
0040: 6b 6d 6f 6e 6f 73 2e 6e 65 74 2f 6e 79 73 6c 2f  kmonos.net/nysl/
0050: 0d 0a 20 2a 0d 0a 20 2a 20 45 72 72 6f 72 20 49  .. *.. * Error I
0060: 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 50  nformation for P
0070: 6f 6c 65 6d 79 20 50 72 6f 67 72 61 6d 6d 69 6e  olemy Programmin
0080: 67 20 4c 61 6e 67 75 61 67 65 0d 0a 20 2a 2f 0d  g Language.. */.
0090: 0a 6d 6f 64 75 6c 65 20 70 6f 6c 65 6d 79 2e 66  .module polemy.f
00a0: 61 69 6c 75 72 65 3b 0d 0a 69 6d 70 6f 72 74 20  ailure;..import 
00b0: 70 6f 6c 65 6d 79 2e 5f 63 6f 6d 6d 6f 6e 3b 0d  polemy._common;.
00c0: 0a 0d 0a 2f 2f 2f 20 52 65 70 72 65 73 65 6e 74  .../// Represent
00d0: 73 20 61 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  s a position in 
00e0: 73 6f 75 72 63 65 20 63 6f 64 65 73 0d 0a 0d 0a  source codes....
00f0: 61 6c 69 61 73 20 69 6d 6d 75 74 61 62 6c 65 28  alias immutable(
0100: 4c 65 78 50 6f 73 69 74 69 6f 6e 5f 74 29 20 4c  LexPosition_t) L
0110: 65 78 50 6f 73 69 74 69 6f 6e 3b 0d 0a 0d 0a 2f  exPosition;..../
0120: 2f 2f 20 52 65 70 72 65 73 65 6e 74 73 20 61 20  // Represents a 
0130: 70 6f 73 69 74 69 6f 6e 20 69 6e 20 73 6f 75 72  position in sour
0140: 63 65 20 63 6f 64 65 73 0d 0a 0d 0a 63 6c 61 73  ce codes....clas
0150: 73 20 4c 65 78 50 6f 73 69 74 69 6f 6e 5f 74 0d  s LexPosition_t.
0160: 0a 7b 0d 0a 09 69 6d 6d 75 74 61 62 6c 65 20 73  .{...immutable s
0170: 74 72 69 6e 67 20 66 69 6c 65 6e 61 6d 65 3b 20  tring filename; 
0180: 2f 2f 2f 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  /// name of the 
0190: 73 6f 75 72 63 65 20 66 69 6c 65 0d 0a 09 69 6d  source file...im
01a0: 6d 75 74 61 62 6c 65 20 69 6e 74 20 20 20 20 6c  mutable int    l
01b0: 69 6e 65 6e 6f 3b 20 20 20 2f 2f 2f 20 31 2d 6f  ineno;   /// 1-o
01c0: 72 69 67 69 6e 0d 0a 09 69 6d 6d 75 74 61 62 6c  rigin...immutabl
01d0: 65 20 69 6e 74 20 20 20 20 63 6f 6c 75 6d 6e 3b  e int    column;
01e0: 20 20 20 2f 2f 2f 20 31 2d 6f 72 69 67 69 6e 0d     /// 1-origin.
01f0: 0a 0d 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65  ....mixin Simple
0200: 43 6c 61 73 73 3b 0d 0a 09 6f 76 65 72 72 69 64  Class;...overrid
0210: 65 20 73 74 72 69 6e 67 20 74 6f 53 74 72 69 6e  e string toStrin
0220: 67 28 29 20 63 6f 6e 73 74 0d 0a 09 7b 0d 0a 09  g() const...{...
0230: 09 72 65 74 75 72 6e 20 73 70 72 69 6e 74 66 21  .return sprintf!
0240: 28 22 25 73 3a 25 64 3a 25 64 22 29 28 66 69 6c  ("%s:%d:%d")(fil
0250: 65 6e 61 6d 65 2c 20 6c 69 6e 65 6e 6f 2c 20 63  ename, lineno, c
0260: 6f 6c 75 6d 6e 29 3b 0d 0a 09 7d 0d 0a 0d 0a 09  olumn);...}.....
0270: 73 74 61 74 69 63 20 4c 65 78 50 6f 73 69 74 69  static LexPositi
0280: 6f 6e 20 64 75 6d 6d 79 3b 0d 0a 09 73 74 61 74  on dummy;...stat
0290: 69 63 20 74 68 69 73 28 29 7b 20 64 75 6d 6d 79  ic this(){ dummy
02a0: 20 3d 20 6e 65 77 20 4c 65 78 50 6f 73 69 74 69   = new LexPositi
02b0: 6f 6e 28 22 3c 75 6e 6e 61 6d 65 64 3e 22 2c 30  on("<unnamed>",0
02c0: 2c 30 29 3b 20 7d 0d 0a 7d 0d 0a 0d 0a 75 6e 69  ,0); }..}....uni
02d0: 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20  ttest..{...auto 
02e0: 70 20 3d 20 6e 65 77 20 4c 65 78 50 6f 73 69 74  p = new LexPosit
02f0: 69 6f 6e 28 22 68 65 6c 6c 6f 2e 63 70 70 22 2c  ion("hello.cpp",
0300: 20 31 32 33 2c 20 34 35 29 3b 0d 0a 0d 0a 09 61   123, 45);.....a
0310: 73 73 65 72 74 5f 65 71 28 20 70 2e 66 69 6c 65  ssert_eq( p.file
0320: 6e 61 6d 65 2c 20 22 68 65 6c 6c 6f 2e 63 70 70  name, "hello.cpp
0330: 22 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71  " );...assert_eq
0340: 28 20 70 2e 6c 69 6e 65 6e 6f 2c 20 31 32 33 20  ( p.lineno, 123 
0350: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20  );...assert_eq( 
0360: 70 2e 63 6f 6c 75 6d 6e 2c 20 34 35 20 29 3b 0d  p.column, 45 );.
0370: 0a 09 61 73 73 65 72 74 5f 65 71 28 20 74 65 78  ..assert_eq( tex
0380: 74 28 70 29 2c 20 22 68 65 6c 6c 6f 2e 63 70 70  t(p), "hello.cpp
0390: 3a 31 32 33 3a 34 35 22 20 29 3b 0d 0a 0d 0a 09  :123:45" );.....
03a0: 61 73 73 65 72 74 28 20 21 5f 5f 74 72 61 69 74  assert( !__trait
03b0: 73 28 63 6f 6d 70 69 6c 65 73 2c 20 6e 65 77 20  s(compiles, new 
03c0: 4c 65 78 50 6f 73 69 74 69 6f 6e 29 20 29 3b 0d  LexPosition) );.
03d0: 0a 09 61 73 73 65 72 74 28 20 21 5f 5f 74 72 61  ..assert( !__tra
03e0: 69 74 73 28 63 6f 6d 70 69 6c 65 73 2c 20 70 2e  its(compiles, p.
03f0: 66 69 6c 65 6e 61 6d 65 3d 22 66 6f 6f 22 29 20  filename="foo") 
0400: 29 3b 0d 0a 09 61 73 73 65 72 74 28 20 21 5f 5f  );...assert( !__
0410: 74 72 61 69 74 73 28 63 6f 6d 70 69 6c 65 73 2c  traits(compiles,
0420: 20 70 2e 6c 69 6e 65 6e 6f 20 20 3d 37 38 39 29   p.lineno  =789)
0430: 20 29 3b 0d 0a 09 61 73 73 65 72 74 28 20 21 5f   );...assert( !_
0440: 5f 74 72 61 69 74 73 28 63 6f 6d 70 69 6c 65 73  _traits(compiles
0450: 2c 20 70 2e 63 6f 6c 75 6d 6e 20 20 3d 32 32 32  , p.column  =222
0460: 29 20 29 3b 0d 0a 0d 0a 09 61 75 74 6f 20 71 20  ) );.....auto q 
0470: 3d 20 6e 65 77 20 4c 65 78 50 6f 73 69 74 69 6f  = new LexPositio
0480: 6e 28 22 68 65 6c 6c 6f 2e 63 70 70 22 2c 20 31  n("hello.cpp", 1
0490: 32 33 2c 20 34 36 29 3b 0d 0a 09 61 73 73 65 72  23, 46);...asser
04a0: 74 5f 6c 74 28 20 70 2c 20 71 20 29 3b 0d 0a 09  t_lt( p, q );...
04b0: 61 73 73 65 72 74 5f 6e 65 28 20 70 2c 20 71 20  assert_ne( p, q 
04c0: 29 3b 0d 0a 7d 0d 0a 0d 0a 2f 2a 6d 69 78 69 6e  );..}..../*mixin
04d0: 2a 2f 0d 0a 74 65 6d 70 6c 61 74 65 20 45 78 63  */..template Exc
04e0: 65 70 74 69 6f 6e 57 69 74 68 50 6f 73 69 74 69  eptionWithPositi
04f0: 6f 6e 28 29 0d 0a 7b 0d 0a 09 4c 65 78 50 6f 73  on()..{...LexPos
0500: 69 74 69 6f 6e 20 70 6f 73 3b 0d 0a 09 74 68 69  ition pos;...thi
0510: 73 28 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70  s( LexPosition p
0520: 6f 73 2c 20 73 74 72 69 6e 67 20 6d 73 67 2c 20  os, string msg, 
0530: 73 74 72 69 6e 67 20 66 69 6c 65 3d 6e 75 6c 6c  string file=null
0540: 2c 20 73 69 7a 65 5f 74 20 6c 69 6e 65 3d 30 2c  , size_t line=0,
0550: 20 54 68 72 6f 77 61 62 6c 65 20 6e 65 78 74 3d   Throwable next=
0560: 6e 75 6c 6c 20 29 0d 0a 09 7b 0d 0a 09 09 69 66  null )...{....if
0570: 28 70 6f 73 20 69 73 20 6e 75 6c 6c 29 0d 0a 09  (pos is null)...
0580: 09 09 73 75 70 65 72 28 73 70 72 69 6e 74 66 21  ..super(sprintf!
0590: 28 22 5b 3f 3f 5d 20 25 73 22 29 28 6d 73 67 29  ("[??] %s")(msg)
05a0: 2c 20 66 69 6c 65 2c 20 6c 69 6e 65 2c 20 6e 65  , file, line, ne
05b0: 78 74 29 3b 0d 0a 09 09 65 6c 73 65 0d 0a 09 09  xt);....else....
05c0: 09 73 75 70 65 72 28 73 70 72 69 6e 74 66 21 28  .super(sprintf!(
05d0: 22 5b 25 73 5d 20 25 73 22 29 28 70 6f 73 2c 20  "[%s] %s")(pos, 
05e0: 6d 73 67 29 2c 20 66 69 6c 65 2c 20 6c 69 6e 65  msg), file, line
05f0: 2c 20 6e 65 78 74 29 3b 0d 0a 09 09 74 68 69 73  , next);....this
0600: 2e 70 6f 73 20 3d 20 70 6f 73 3b 0d 0a 09 7d 0d  .pos = pos;...}.
0610: 0a 7d 0d 0a 0d 0a 63 6c 61 73 73 20 55 6e 65 78  .}....class Unex
0620: 70 65 63 74 65 64 45 4f 46 20 3a 20 45 78 63 65  pectedEOF : Exce
0630: 70 74 69 6f 6e 20 7b 20 6d 69 78 69 6e 20 45 78  ption { mixin Ex
0640: 63 65 70 74 69 6f 6e 57 69 74 68 50 6f 73 69 74  ceptionWithPosit
0650: 69 6f 6e 3b 20 7d 20 2f 2f 2f 20 45 4f 46 20 64  ion; } /// EOF d
0660: 75 72 69 6e 67 20 6c 65 78 69 6e 67 2f 70 61 72  uring lexing/par
0670: 73 69 6e 67 0d 0a 63 6c 61 73 73 20 4c 65 78 45  sing..class LexE
0680: 78 63 65 70 74 69 6f 6e 20 3a 20 45 78 63 65 70  xception : Excep
0690: 74 69 6f 6e 20 7b 20 6d 69 78 69 6e 20 45 78 63  tion { mixin Exc
06a0: 65 70 74 69 6f 6e 57 69 74 68 50 6f 73 69 74 69  eptionWithPositi
06b0: 6f 6e 3b 20 7d 20 2f 2f 2f 20 4c 65 78 65 72 20  on; } /// Lexer 
06c0: 65 72 72 6f 72 73 0d 0a 63 6c 61 73 73 20 50 61  errors..class Pa
06d0: 72 73 65 45 78 63 65 70 74 69 6f 6e 20 3a 20 45  rseException : E
06e0: 78 63 65 70 74 69 6f 6e 20 7b 20 6d 69 78 69 6e  xception { mixin
06f0: 20 45 78 63 65 70 74 69 6f 6e 57 69 74 68 50 6f   ExceptionWithPo
0700: 73 69 74 69 6f 6e 3b 20 7d 20 2f 2f 2f 20 50 61  sition; } /// Pa
0710: 72 73 65 72 20 65 72 72 6f 72 73 0d 0a 63 6c 61  rser errors..cla
0720: 73 73 20 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ss RuntimeExcept
0730: 69 6f 6e 20 3a 20 45 78 63 65 70 74 69 6f 6e 20  ion : Exception 
0740: 7b 20 6d 69 78 69 6e 20 45 78 63 65 70 74 69 6f  { mixin Exceptio
0750: 6e 57 69 74 68 50 6f 73 69 74 69 6f 6e 3b 20 7d  nWithPosition; }
0760: 20 2f 2f 2f 20 45 76 61 6c 75 61 74 6f 72 20 65   /// Evaluator e
0770: 72 72 6f 72 73 0d 0a                             rrors..