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

86
LINES

< > BotCompany Repo | #1033967 // SimpleLeftToRightParser

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

Transpiled version (20441L) is out of date.

sclass SimpleLeftToRightParser {
  S text;
  LS tok;
  ListAndIndex<S> ptr, mainLoopPtr;
  S currentToken;
  bool caseInsensitive;
  new L warnings;
  
  *() {}
  *(S *text) {}
  *(LS *tok) {}
  
  S t aka token() { ret currentToken; }
  S token(int i) { ret get(tok, ptr.idx()+i*2); }
  S tpp() { var t = t(); next(); ret t; }
  S lastSpace() { ret get(tok, ptr.idx()-1); }
  S nextSpace() { ret get(tok, ptr.idx()+1); }
  
  bool is(S t) { ret eqOrEqic(caseInsensitive, currentToken, t); }
  bool was(S t) { ret eqOrEqic(caseInsensitive, token(-1), t); }
  
  meta-for isInteger also as isIdentifier {
    bool isInteger() { ret isInteger(t()); }
    bool isInteger(S s) { ret main isInteger(s); }
  }
  
  void next aka consume() { if (!endOfText()) ptr(ptr.plus(2)); }
  
  void ptr(ListAndIndex<S> ptr) { this.ptr = ptr; fetch(); }
  int idx() { ret ptr.idx(); }
  int lTok() { ret l(tok); }

  bool atEnd aka endOfText() { ret ptr.atEnd(); }
    
  void fetch { currentToken = ptr!; }
  
  bool lineBreak() { ret containsLineBreak(get(tok, ptr.idx()-1)); }
  bool atEndOrLineBreak() { ret atEnd() || lineBreak(); }
  
  void init {
    tok if null = javaTok(text);
    if (ptr == null) ptr(ListAndIndex(tok, 1));
  }
  
  bool mainLoop() {
    init();
    if (atEnd()) false;
    if (eq(mainLoopPtr, ptr))
      fail("main loop didn't advance (current token: " + quote(token()) + ")");
    mainLoopPtr = ptr;
    true;
  }
  
  void unknownToken {
    warn("Unknown token: " + t());
  }
  
  void warn(S msg) {
    warnings.add(print(msg));
  }
  
  void next(int n) { 
    ptr(ptr.idx()+n*2);
  }
  
  // if i points to an N token, it is incremented
  void ptr(int i) {
    ptr(ListAndIndex(tok, min(i | 1, l(tok))));
  }
  
  LineAndColumn lineAndColumn() {
    ret tokenToLineAndColumn(ptr);
  }
  
  S consumeUntilSpaceOr(IF0<Bool> pred) {
    int i = idx();
    do next(); while (!atEnd() && empty(lastSpace()) && !pred!);
    ret joinSubList(tok, i, idx()-1);
  }
  
  void setText(S text) {
    this.text = text;
    tok = null;
    ptr = null;
  }
}

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/34
Text MD5: b690c4c60e143ffebbc1e084e9e05697
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-25 17:27:15
Source code size: 2148 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 76 / 214
Version history: 33 change(s)
Referenced in: [show references]