comparison core/src/luan/impl/Parser.java @ 679:43522473599d

make java line numbers match
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Apr 2016 15:19:25 -0600
parents 859c0dedc8b6
children b620b8e1010f
comparison
equal deleted inserted replaced
678:49f3d290bebd 679:43522473599d
1 package luan.impl; 1 package luan.impl;
2 2
3 3
4 final class Parser { 4 final class Parser {
5
6 private static class Frame {
7 int i;
8 StringBuilder sb;
9 }
10
5 // private final LuanSource src; 11 // private final LuanSource src;
6 public final String sourceName; 12 public final String sourceName;
7 public final String text; 13 public final String text;
8 private final int len; 14 private final int len;
9 private int[] stack = new int[256]; 15 private Frame[] stack = new Frame[256];
10 private int frame = 0; 16 private int frame = 0;
11 private int iHigh; 17 private int iHigh;
12 18
13 public Parser(String sourceName,String text) { 19 public Parser(String sourceName,String text) {
14 // this.src = src; 20 // this.src = src;
15 this.sourceName = sourceName; 21 this.sourceName = sourceName;
16 this.text = text; 22 this.text = text;
17 this.len = text.length(); 23 this.len = text.length();
24 stack[0] = new Frame();
18 } 25 }
19 26
20 private int i() { 27 private int i() {
21 return stack[frame]; 28 return stack[frame].i;
22 } 29 }
23 30
24 private void i(int i) { 31 private void i(int i) {
25 stack[frame] += i; 32 Frame f = stack[frame];
26 if( iHigh < stack[frame] ) 33 f.i += i;
27 iHigh = stack[frame]; 34 if( iHigh < f.i )
35 iHigh = f.i;
28 } 36 }
29 37
30 public int begin() { 38 public int begin() {
31 frame++; 39 frame++;
32 if( frame == stack.length ) { 40 if( frame == stack.length ) {
33 int[] a = new int[2*frame]; 41 Frame[] a = new Frame[2*frame];
34 System.arraycopy(stack,0,a,0,frame); 42 System.arraycopy(stack,0,a,0,frame);
35 stack = a; 43 stack = a;
36 } 44 }
37 stack[frame] = stack[frame-1]; 45 Frame f = new Frame();
46 f.i = stack[frame-1].i;
47 stack[frame] = f;
38 return i(); 48 return i();
39 } 49 }
40 50
41 public void rollback() { 51 public void rollback() {
42 stack[frame] = stack[frame-1]; 52 Frame f = stack[frame];
53 f.i = stack[frame-1].i;
54 f.sb = null;
43 } 55 }
44 56
45 public <T> T success(T t) { 57 public <T> T success(T t) {
46 success(); 58 success();
47 return t; 59 return t;
48 } 60 }
49 61
50 public boolean success() { 62 public boolean success() {
63 Frame f = stack[frame];
64 if( f.sb != null && f.sb.length() > 0 ) throw new RuntimeException("sb not emtpy");
51 frame--; 65 frame--;
52 stack[frame] = stack[frame+1]; 66 stack[frame].i = f.i;
53 return true; 67 return true;
54 } 68 }
55 69
56 public <T> T failure(T t) { 70 public <T> T failure(T t) {
57 failure(); 71 failure();
67 return new ParseException(msg,sourceName,text,i(),iHigh); 81 return new ParseException(msg,sourceName,text,i(),iHigh);
68 } 82 }
69 83
70 public ParseException exception() { 84 public ParseException exception() {
71 return exception("Invalid input"); 85 return exception("Invalid input");
86 }
87
88 public StringBuilder sb() {
89 Frame f = stack[frame];
90 if( f.sb == null )
91 f.sb = new StringBuilder();
92 return f.sb;
93 }
94
95 public void upSb() {
96 Frame f = stack[frame];
97 StringBuilder sb = f.sb;
98 if( sb != null && sb.length() > 0 ) {
99 Frame fUp = stack[frame-1];
100 if( fUp.sb == null )
101 fUp.sb = sb;
102 else
103 fUp.sb.append(sb.toString());
104 f.sb = null;
105 }
72 } 106 }
73 107
74 public int currentIndex() { 108 public int currentIndex() {
75 return i(); 109 return i();
76 } 110 }