All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unlaxer.tinyexpression.parser.SideEffectStringExpressionParser Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package org.unlaxer.tinyexpression.parser;

import java.util.List;

import org.unlaxer.Token;
import org.unlaxer.parser.Parser;
import org.unlaxer.parser.Parsers;
import org.unlaxer.parser.ascii.LeftParenthesisParser;
import org.unlaxer.parser.ascii.RightParenthesisParser;
import org.unlaxer.parser.elementary.WordParser;
import org.unlaxer.tinyexpression.CalculationContext;
import org.unlaxer.tinyexpression.evaluator.javacode.TinyExpressionTokens;
import org.unlaxer.tinyexpression.parser.javalang.JavaStyleDelimitedLazyChain;
import org.unlaxer.util.annotation.TokenExtractor;

public class SideEffectStringExpressionParser extends JavaStyleDelimitedLazyChain
    implements NumberExpression {

  private static final long serialVersionUID = 6172097671148475538L;


  public SideEffectStringExpressionParser() {
    super();
  }

  @Override
  public org.unlaxer.parser.Parsers getLazyParsers() {
    return
        new Parsers(Parser.get(SideEffectNameParser.class),
            Parser.get(() -> new WordParser(":")), Parser.get(JavaClassMethodParser.class), // 2
            Parser.get(LeftParenthesisParser.class),
            Parser.get(SideEffectStringExpressionParameterParser.class), // 4
            Parser.get(RightParenthesisParser.class)
        );
  }

  @TokenExtractor 
  public static Token getMethodClause(Token thisParserParsed) {
    return thisParserParsed.getChildWithParser(JavaClassMethodParser.class); //2;
  }

  @TokenExtractor 
  public static Token getParametersClause(Token thisParserParsed) {
    return thisParserParsed.getChildWithParser(SideEffectStringExpressionParameterParser.class); //4
  }

  @TokenExtractor
  public static MethodAndParameters extract(Token token , TinyExpressionTokens tinyExpressionTokens) {

    Token classMethodToken = getMethodClause(token);// TODO

    ClassNameAndIdentifier extract = ((ClassNameAndIdentifierExtractor)classMethodToken.parser)
        .extractClassNameAndIdentifier(classMethodToken, tinyExpressionTokens);

    Token parameter = getParametersClause(token);

    SideEffectStringExpressionParameterParser sideEffectStringExpressionParameterParser =
        Parser.get(SideEffectStringExpressionParameterParser.class);

    List parameterTokens = sideEffectStringExpressionParameterParser.parameterTokens(parameter);

    return new MethodAndParameters(extract, parameterTokens);
  }

  public static class MethodAndParameters {
    public final ClassNameAndIdentifier classNameAndIdentifier;
    public final List parameterTokens;
    public final Class[] parameterTypes;

    public MethodAndParameters(ClassNameAndIdentifier classNameAndIdentifier,
        List parameterTokens) {
      super();
      this.classNameAndIdentifier = classNameAndIdentifier;
      this.parameterTokens = parameterTokens;
      parameterTypes = new Class[parameterTokens.size() + 2];
      parameterTypes[0] = CalculationContext.class;
      parameterTypes[1] = String.class;
      int i = 2;
      for (Token token : parameterTokens) {
        Parser parser = token.parser;
        parameterTypes[i] = parser instanceof NumberExpression ? float.class
            : parser instanceof BooleanExpression ? boolean.class
                : parser instanceof StringExpression ? String.class : null;
        i++;
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy