Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker-Parser 0.9: #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Linker-Parser 0.9: #5

wants to merge 3 commits into from

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.

2 participants