org.unlaxer.tinyexpression.parser.TinyExpressionParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tinyExpression Show documentation
Show all versions of tinyExpression Show documentation
TinyExpression implemented with Unlaxer
The newest version!
package org.unlaxer.tinyexpression.parser;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.unlaxer.Parsed;
import org.unlaxer.Token;
import org.unlaxer.Token.ChildrenKind;
import org.unlaxer.Token.ScanDirection;
import org.unlaxer.TokenKind;
import org.unlaxer.TokenPredicators;
import org.unlaxer.TypedToken;
import org.unlaxer.context.ParseContext;
import org.unlaxer.parser.AfterParse;
import org.unlaxer.parser.Parser;
import org.unlaxer.parser.Parsers;
import org.unlaxer.parser.RootParserIndicator;
import org.unlaxer.tinyexpression.parser.javalang.AnnotationsParser;
import org.unlaxer.tinyexpression.parser.javalang.CodesParser;
import org.unlaxer.tinyexpression.parser.javalang.ImportsParser;
import org.unlaxer.tinyexpression.parser.javalang.JavaStyleDelimitedLazyChain;
import org.unlaxer.tinyexpression.parser.javalang.VariableDeclarationsParser;
import org.unlaxer.tinyexpression.parser.javalang.CodeParser.CodeBlock;
import org.unlaxer.util.annotation.TokenExtractor;
import org.unlaxer.util.annotation.TokenExtractor.Timing;
@SuppressWarnings("serial")
public class TinyExpressionParser extends JavaStyleDelimitedLazyChain implements RootParserIndicator ,
AfterParse{
@Override
public org.unlaxer.parser.Parsers getLazyParsers() {
return new Parsers(
Parser.get(CodesParser.class),
Parser.get(ImportsParser.class),
Parser.get(VariableDeclarationsParser.class),
Parser.get(AnnotationsParser.class),
new org.unlaxer.parser.combinator.Optional(
ExpressionsParser.class
),
Parser.get(MethodsParser.class)
);
}
@Override
public Parsed parse(ParseContext parseContext, TokenKind tokenKind, boolean invertMatch) {
Parsed parsed = super.parse(parseContext, tokenKind, invertMatch);
return afterParse(parseContext, parsed, tokenKind, invertMatch);
}
// @Override
// public Parsed afterParse(ParseContext parseContext, Parsed parsed, TokenKind tokenKind, boolean invertMatch) {
// return parsed;
// }
@Override
public Parsed afterParse(ParseContext parseContext, Parsed parsed, TokenKind tokenKind, boolean invertMatch) {
if(parsed.isSucceeded()) {
Token rootToken = parsed.getRootToken(true);
Optional child1 = rootToken.getChildAsOptional(TokenPredicators.parsers(ExpressionsParser.class));
Optional child2 = rootToken.getChildAsOptional(TokenPredicators.parsers(MethodsParser.class));
if(child1.isEmpty() && child2.isEmpty()) {
parsed = parsed.negate().setMessage("specify method or expression");
}
}
return parsed;
}
/**
* @param thisParserParsed
* @return CodeBlock List
*/
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static List extractCodeBlocksAsModel(Token thisParserParsed){
Optional childWithParserAsOptional = thisParserParsed.getChildWithParserAsOptional(CodesParser.class);
List codeBlocks = childWithParserAsOptional
.map(CodesParser::extractCodeBlocksAsModel)
.orElseGet(List::of);
return codeBlocks;
}
/**
* @param thisParserParsed
* @return CodeBlock List
*/
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static List extractCodes(Token thisParserParsed){
Optional childWithParserAsOptional = thisParserParsed.getChildWithParserAsOptional(CodesParser.class);
List importChildren = childWithParserAsOptional
.map(CodesParser::extractCodeBlocks)
.orElseGet(List::of);
return importChildren;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static Token extractCodesToken(Token thisParserParsed){
List codesChildren = extractCodes(thisParserParsed);
Token codes = new Token(TokenKind.consumed, codesChildren, Parser.get(CodesParser.class),0);
return codes;
}
/**
* @param thisParserParsed
* @return token of restructure ImportsParser
*/
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static List extractImports(Token thisParserParsed){
Optional childWithParserAsOptional = thisParserParsed.getChildWithParserAsOptional(ImportsParser.class);
List importChildren = childWithParserAsOptional
.map(ImportsParser::extractImports)
.orElseGet(List::of);
return importChildren;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static Token extractImportsToken(Token thisParserParsed){
List importChildren = extractImports(thisParserParsed);
Token imports = new Token(TokenKind.consumed, importChildren, Parser.get(ImportsParser.class),0);
return imports;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static Token extractExpression(Token thisParserParsed){
Token childWithParser = thisParserParsed.getChildWithParser(ExpressionsParser.class);
Token extractExpressionToken = ExpressionsParser.extractExpressionToken(childWithParser);
return extractExpressionToken;
}
@TokenExtractor(timings = Timing.UseOperatorOperandTree)
public static TypedToken extractExpressionWithAfterReduced(Token thisParserParsed){
TypedToken extractExpressionToken = thisParserParsed.getChildWithParser(
_parser->_parser instanceof ExpressionInterface)
.typed(ExpressionInterface.class);
return extractExpressionToken;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static List extractVariables(Token tinyExpressionToken) {
Parser.checkTokenParsedBySpecifiedParser(tinyExpressionToken , TinyExpressionParser.class);
Optional childWithParserAsOptional = tinyExpressionToken.getChildWithParserAsOptional(VariableDeclarationsParser.class);
List variableChildren = childWithParserAsOptional
.map(VariableDeclarationsParser::extractVariables)
.orElseGet(List::of);
return variableChildren;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static List*Typed*/Token/**/> extractMethods(/*Typed*/Token/**/ tinyExpressionToken) {
Parser.checkTokenParsedBySpecifiedParser(tinyExpressionToken , TinyExpressionParser.class);
Optional*Typed*/Token/**/> childWithParserAsOptional =
tinyExpressionToken.getChildWithParserAsOptional/*Typed*/(MethodsParser.class);
List*Typed*/Token/**/> methodChildren = childWithParserAsOptional
.map(MethodsParser::extractMethods)
.orElseGet(List::of);
return methodChildren;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static Token extractVariablesToken(Token tinyExpressionToken) {
Parser.checkTokenParsedBySpecifiedParser(tinyExpressionToken , TinyExpressionParser.class);
List variableChildren = extractVariables(tinyExpressionToken);
Token variables = new Token(TokenKind.consumed, variableChildren, Parser.get(VariableDeclarationsParser.class),0);
return variables;
}
public static List extractAnnotaions(Token tinyExpressionToken) {
Optional childWithParserAsOptional = tinyExpressionToken.getChildWithParserAsOptional(AnnotationsParser.class);
List annotationChildren = childWithParserAsOptional
.map(AnnotationsParser::extractAnnotationss)
.orElseGet(List::of);
return annotationChildren;
}
public static Token extractAnnotaionsToken(Token tinyExpressionToken) {
List extractAnnotaions = extractAnnotaions(tinyExpressionToken);
Token variables = new Token(TokenKind.consumed, extractAnnotaions, Parser.get(AnnotationsParser.class),0);
return variables;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static Token extractMethodsToken(TypedToken tinyExpressionToken) {
Parser.checkTokenParsedBySpecifiedParser(tinyExpressionToken , TinyExpressionParser.class);
List*Typed*/Token/**/> methodChildren = extractMethods(tinyExpressionToken);
/*Typed*/Token/**/ methods = new /*Typed*/Token/*<>*/(TokenKind.consumed, methodChildren, Parser.get(MethodsParser.class),0);
return methods;
}
@TokenExtractor(timings = Timing.CreateOperatorOperandTree)
public static Optional returningTypeHint(Token tinyExpressionToken , String methodName){
Parser.checkTokenParsedBySpecifiedParser(tinyExpressionToken , TinyExpressionParser.class);
Token child = tinyExpressionToken.getChild(TokenPredicators.parsers(MethodsParser.class));
List methods = child.flatten(ScanDirection.Breadth, ChildrenKind.astNodes).stream()
.filter(TokenPredicators.parserImplements(MethodParser.class))
.collect(Collectors.toList());
for (Token token : methods) {
TypedToken methodParserToken = token.typed(MethodParser.class);
MethodParser parser = methodParserToken.getParser();
String methodNameOfChild = parser.methodName(methodParserToken).getToken().get();
if(methodName.equals(methodNameOfChild)) {
return Optional.of( token.getChildWithParser(parser.returningParser()));
}
}
return Optional.empty();
}
}