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

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

The newest version!
package org.unlaxer.tinyexpression.parser;

import java.util.List;

import org.unlaxer.Name;
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 SideEffectStringToBooleanExpressionParser extends JavaStyleDelimitedLazyChain implements NumberExpression {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	
	public SideEffectStringToBooleanExpressionParser() {
		super();
	}

	public SideEffectStringToBooleanExpressionParser(Name name) {
		super(name);
	}
		
	public static MethodAndParameters extract(Token token , TinyExpressionTokens tinyExpressionTokens) {
		Token classMethodToken = getMethodClause(token);
		Token parameter = getParametersClause(token);
		
    ClassNameAndIdentifier extract = ((ClassNameAndIdentifierExtractor)classMethodToken.parser)
        .extractClassNameAndIdentifier(classMethodToken, tinyExpressionTokens);

		SideEffectStringToBooleanExpressionParameterParser sideEffectStringToBooleanExpressionParameterParser = 
				Parser.get(SideEffectStringToBooleanExpressionParameterParser.class);
		List parameterTokens = sideEffectStringToBooleanExpressionParameterParser.parameterTokens(parameter);
		return new MethodAndParameters(extract, parameterTokens);
	}

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

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

	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;
//			this.parmeterTypes = parmeterTypes;
			
			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++;
			}
		}
	}

	@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(SideEffectStringToBooleanExpressionParameterParser.class),//4
        Parser.get(RightParenthesisParser.class)
      );    

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy