Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

165
LINES

< > BotCompany Repo | #1033967 // SimpleLeftToRightParser

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (22797L) is out of date.

1  
sclass SimpleLeftToRightParser > Meta {
2  
  S text;
3  
  LS tok;
4  
  gettable TokPtr ptr;
5  
  TokPtr mainLoopPtr;
6  
  S currentToken;
7  
  bool caseInsensitive;
8  
  new L warnings;
9  
  
10  
  settable bool internIdentifiers;
11  
  
12  
  *() {}
13  
  *(S *text) {}
14  
  *(LS *tok) {}
15  
  
16  
  swappable LS tokenize(S text) { ret javaTok(text); }
17  
  
18  
  S t aka token() { ret currentToken; }
19  
  S token(int i) { ret get(tok, ptr.idx()+i*2); }
20  
  S next aka consume aka tpp() { var t = t(); next(1); ret t; }
21  
  S lastSpace aka prevSpace() { ret get(tok, ptr.idx()-1); }
22  
  S nextSpace() { ret get(tok, ptr.idx()+1); }
23  
  S space(int i) { ret get(tok, ptr.idx()+i*2+1); }
24  
  
25  
  void unconsume() { next(-1); }
26  
  
27  
  bool tokEq aka eqTok(S a, S b) { ret eqOrEqic(caseInsensitive, a, b); }
28  
  bool tokEqOneOf(S a, S... l) { ret any(l, b -> tokEq(a, b)); }
29  
  
30  
  bool is(int i, S t) { ret tokEq(token(i), t); }
31  
  bool is(S t) { ret tokEq(currentToken, t); }
32  
  bool was(S t) { ret tokEq(token(-1), t); }
33  
  
34  
  bool isOneOf(S... tokens) { ret tokEqOneOf(currentToken, tokens); }
35  
  
36  
  S[] consumeArray(int n) {
37  
    S[] array = new[n];
38  
    for i to n: array[i] = consume();
39  
    ret array;
40  
  }
41  
  
42  
  meta-for isInteger also as isIdentifier {
43  
    bool isInteger() { ret isInteger(t()); }
44  
    bool isInteger(S s) { ret main isInteger(s); }
45  
  }
46  
  
47  
  S consumeIdentifier() {
48  
    S s = assertIdentifier(consume());
49  
    if (internIdentifiers)
50  
      s = intern(s);
51  
    ret s;
52  
  }
53  
  
54  
  S consumeIdentifierOpt() { ret isIdentifier(t()) ? consumeIdentifier() : null; }
55  
  
56  
  int consumeInteger() { ret parseInt(assertInteger(consume())); }
57  
  
58  
  bool consumeOpt(S token) {
59  
    if (!is(token)) false;
60  
    consume();
61  
    true;
62  
  }
63  
  
64  
  void consume(S token) {
65  
    if (!is(token))
66  
      fail("Expected " + quote(token) + ", got " + describeToken(token());
67  
    consume();
68  
  }
69  
  
70  
  S describeToken(S token) {
71  
    ret token == null ? "EOF" : quote(token);
72  
  }
73  
  
74  
  S consumeOneOf(S... tokens) {
75  
    if (!isOneOf(tokens))
76  
      fail("Expected one of " + asList(tokens));
77  
    ret consume();
78  
  }
79  
  
80  
  void ptr(TokPtr ptr) { this.ptr = ptr; fetch(); }
81  
  int idx aka tokIdx() { ret ptr.idx(); }
82  
  int lTok() { ret l(tok); }
83  
  int nRemainingTokens() { ret (lTok()-idx())/2; }
84  
85  
  bool atEnd aka endOfText() { ret ptr.atEnd(); }
86  
    
87  
  void fetch { currentToken = ptr!; }
88  
  
89  
  bool lineBreak() { ret containsLineBreak(get(tok, ptr.idx()-1)); }
90  
  bool atEndOrLineBreak() { ret atEnd() || lineBreak(); }
91  
  
92  
  void init {
93  
    tok if null = tokenize(text);
94  
    if (ptr == null) ptr(ListAndIndex(tok, 1));
95  
  }
96  
  
97  
  bool mainLoop() {
98  
    init();
99  
    if (atEnd()) false;
100  
    if (eq(mainLoopPtr, ptr))
101  
      fail("main loop didn't advance (current token: " + quote(token()) + ")");
102  
    mainLoopPtr = ptr;
103  
    true;
104  
  }
105  
  
106  
  class AssureAdvance {
107  
    TokPtr cur;
108  
    
109  
    { init(); }
110  
    
111  
    bool get() {
112  
      if (atEnd()) false;
113  
      if (eq(cur, ptr))
114  
        fail("Parse loop didn't advance (current token: " + quote(token()) + ")");
115  
      cur = ptr;
116  
      true;
117  
    }
118  
  }
119  
  
120  
  void unknownToken {
121  
    warn("Unknown token: " + t());
122  
  }
123  
  
124  
  void warn(S msg) {
125  
    warnings.add(print(msg));
126  
  }
127  
  
128  
  // can also move backwards for negative n
129  
  void next aka skip aka consume(int n) { 
130  
    ptr(min(lTok(), ptr.idx()+n*2));
131  
  }
132  
  
133  
  // if i points to an N token, it is incremented
134  
  void ptr(int i) {
135  
    ptr(ListAndIndex(tok, min(i | 1, l(tok))));
136  
  }
137  
  
138  
  LineAndColumn lineAndColumn() {
139  
    ret tokenToLineAndColumn(ptr);
140  
  }
141  
  
142  
  LineAndColumn lineAndColumn(int idx) {
143  
    ret tokenToLineAndColumn(ptr.plus(idx*2));
144  
  }
145  
  
146  
  S consumeUntilSpaceOr(IF0<Bool> pred) {
147  
    int i = idx();
148  
    do next(); while (!atEnd() && empty(lastSpace()) && !pred!);
149  
    ret joinSubList(tok, i, idx()-1);
150  
  }
151  
  
152  
  void setText(S text) {
153  
    this.text = text;
154  
    tok = null;
155  
    ptr = null;
156  
  }
157  
  
158  
  int relativeIndexOf(S token) {
159  
    int n = nRemainingTokens();
160  
    for i to n:
161  
      if (eqTok(token(i), token))
162  
        ret i;
163  
    ret -1;
164  
  }
165  
}

Author comment

Began life as a copy of #1033717

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033967
Snippet name: SimpleLeftToRightParser
Eternal ID of this version: #1033967/68
Text MD5: cc399b5488851a5354d616454363b7ed
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2024-07-14 10:55:35
Source code size: 4098 bytes / 165 lines
Pitched / IR pitched: No / No
Views / Downloads: 395 / 876
Version history: 67 change(s)
Referenced in: [show references]