Skip to content

Conversation

@dmitriic
Copy link
Collaborator

@dmitriic dmitriic commented Oct 17, 2019

  • adds EnumTokens and NumberTokens :
    allows using Enums as simple single-token grammar junctions, simplifying and optimizing handling of sutuations where similar non-composite tokens can occur in the same position;
    allows to define rule fields using types that extend java.lang.Number and have a constructor that accepts a String. Vastly simplifies implementations of java-compatible number type system.
    Example:

    public enum BinaryOperator implements Rule {
      @CapturePattern("-") MINUS,
      @CapturePattern("+") PLUS,
      @CapturePattern("/") SLASH,
      @CapturePattern("*") STAR;
    
      public Number apply(Number leftOperand, Number rightOperand) {
        ...
      }
    }
    public class ExampleBinaryStatement implements Rule {
      protected Number leftOperand;
      protected BinaryOperator operator;
      protected Number rightOperand;
    }
  • adds IgnoreCase annotation
    allows to perform case-insensitive matching using both Pattern and Terminal matchers

  • support for compilers:
    now parser will read rule definition from the first class in inheritance chain that implements Rule interface, ignoring all its children. This allows to reuse grammar rules while implementing different behavior for the resulting AST: one set of grammar extending tokens can be used for evaluation, another - for linting or compiling the application into a different language.

    Example (evaluating token):

    public class ExampleBinaryStatementEvaluator extends ExampleBinaryStatement implements Evaluator<Number> {
      @Override
      public Number evaluate() {
        return operator.apply(leftOperand, rightOperand)
      }
    }

    Example (compiling token):

    public class ExampleBinaryStatementCompiler extends ExampleBinaryStatement implements Compiler<JavaScript> {
    
      @Override
      public JavaScript compile() {
        return new JavaScript()
          .append(leftOperand)
          .append(' ')
          .append(operator)
          .append(' ')
          .append(rightOperand);
      }
    
    }

Dima Chechetkin and others added 3 commits October 17, 2019 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants