Skip to content
ErzengelLichtes edited this page Jan 4, 2020 · 1 revision

Text Parser is a simple system for parsing text directly in code. It does not perform tokenization or any other advanced language parsing as you might find in LEX/YACC and instead leaves that as a responsibility for the user.

Text Parser is as simple as using Has, Check, Expect, and Identifier.

  • Has is used to check to see if some text is at the head and consume it.
  • Check is used to check to see if some text is at the head but will instead leave it there.
  • Expect is used to consume some text, and will throw an error if the text isn't there.
  • Identifier is a class used to read an arbitrary amount of text and then allow the user to either consume it, abandon it, or throw an error.

Example

// text looks like {text,text} or {text,text,}
if(p.Has("{")) { 
    list.Add(p.ReadCStyleIdentifier());
    while(p.Has(",")) {
        if(p.Check("}")) break;
        list.Add(p.ReadCStyleIdentifier());
    }
    p.Expect("}");
}
  1. If there's an opening bracket, it moves the head forward and then enters the conditional block.
  2. It reads text with possible underscores and numbers, adding it to a list. If there's no identifier, it throws an exception (Read* always throws on failure)
  3. It looks for commas to see if there's more identifiers to add.
  4. It checks to see if there's a closing bracket to support trailing commas, but since we have an expect below we don't want to consume it.
  5. It adds any more identifiers to the list.
  6. Once the list has completed (either because there are no more commas, or the trailing comma was followed by a closing bracket) it requires a closing bracket at the head. It will throw an error if there isn't.

This shows the importance and utility of both Has and Check for whether to move the head forward or not.

Clone this wiki locally