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

58
LINES

< > BotCompany Repo | #1033966 // GazelleVScriptParser - successor of GazelleVScript

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

Libraryless. Click here for Pure Java version (5383L/31K).

/*
Input is e.g. "32p 8c". We want to get out the 2 operators.
Then we apply a "puzzler" which tries to combine the operators in a useful way.

// Operators

<int> p = scaleToHeight : IBWntegralImage -> BWImage
<int> c = posterize : BWImage -> BWImage

// Juxtaposition of operators

_ _ = juxtapose : Op, Op -> Op [left-assoc]
*/

sclass GazelleVScriptParser<T> extends SimpleLeftToRightParser {
  CalculatedFieldIndexedList<GAbstractOperator<T>, S> operators = new(op -> op.name);
  new L<ParsedWithTokens> parsed; // contains ints and operators

  *() {}
  *(S text) { super(text); }
  
  { set caseInsensitive; }
  
  void addOperators(GAbstractOperator<T>... l) {
    addAll(operators, l);
  }
  
  void addOperators(Iterable<? extends GAbstractOperator<T>> l) {
    addAll(operators, l);
  }
  
  void parse(S text) { this.text = text; parse(); }
  
  void parse {
    while (mainLoop()) {
      if (isInteger()) {
        addToParse(parseInt(tpp()));
        continue;
      }
      
      addToParse(findOp(tpp()));
    }
  }
  
  // assumes you have already called tpp()
  void addToParse(O o) {
    parsed.add(ParsedWithTokens(o, tok, ptr.idx()-2, ptr.idx()));
  }
  
  GAbstractOperator<T> findOp(S name) {
    var ops = operators.getAllByKey(name);
    if (empty(ops))
      warn("Unknown operator: " + name);
    else if (l(ops) > 1)
      warn("Multiple operator definitions (please use GPolymorphicOperator instead): " + name);
    ret first(ops);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033966
Snippet name: GazelleVScriptParser - successor of GazelleVScript
Eternal ID of this version: #1033966/18
Text MD5: 5b0af4e289f60f88e7dd7dbefa1ee9d5
Transpilation MD5: 13d2c012528423d8b4fafd49ae8fe724
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 02:27:43
Source code size: 1525 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 220 / 362
Version history: 17 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)