org.parboiled.Rule Java Examples

The following examples show how to use org.parboiled.Rule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapSelectionExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Override
@Label("MapSelection Expression")
public Rule ExpressionRule() {
    BinaryOrPrimaryExpressionParser binaryOrPrimaryExpressionParser = parserContext().parser(BinaryOrPrimaryExpressionParser.class);
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);

    return Sequence(
            positionTrackerParser.PushPosition(),
            binaryOrPrimaryExpressionParser.ExpressionRule(),
            spacingParser.Spacing(),
            String("["), spacingParser.Spacing(),
            binaryOrPrimaryExpressionParser.ExpressionRule(),
            spacingParser.Spacing(),
            String("]"),

            push(new MapSelectionExpression(positionTrackerParser.pop(2), binaryOrPrimaryExpressionParser.pop(1), binaryOrPrimaryExpressionParser.pop())),

            MapSelectionExpressionTrail()
    );
}
 
Example #2
Source File: MapSelectionExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
public Rule MapSelectionExpressionTrail() {
    BinaryOrPrimaryExpressionParser binaryOrPrimaryExpressionParser = parserContext().parser(BinaryOrPrimaryExpressionParser.class);
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);

    return Sequence(
            ZeroOrMore(
                    positionTrackerParser.PushPosition(),
                    String("["), spacingParser.Spacing(),
                    binaryOrPrimaryExpressionParser.ExpressionRule(),
                    spacingParser.Spacing(),
                    String("]"),

                    push(new MapSelectionExpression(positionTrackerParser.pop(1), binaryOrPrimaryExpressionParser.pop(1), binaryOrPrimaryExpressionParser.pop()))
            ),
            parserContext().parser(BinaryOperationSuffixExpressionParser.class).ExpressionRule()
    );
}
 
Example #3
Source File: EnumerationListExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Override
@Label("List Enumeration")
public Rule ExpressionRule() {
    MultipleExpressionsParser expressionsParser = parserContext().parser(MultipleExpressionsParser.class);
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),
            String("["),
            spacingParser.Spacing(),
            expressionsParser.Rule(),
            spacingParser.Spacing(),
            Mandatory(String("]"), "Expecting end bracket"),
            push(new EnumeratedListExpression(positionTrackerParser.pop(1), expressionsParser.pop()))
    );
}
 
Example #4
Source File: CfgPropsParboiled.java    From nb-springboot with Apache License 2.0 6 votes vote down vote up
Rule kvPair() {
    return Sequence(
            FirstOf(
                    Sequence(
                            key(),
                            keyStoreProp,
                            Optional(whitespace()),
                            separator(),
                            Optional(whitespace()),
                            Sequence(value(), valueStoreProp)
                    ),
                    Sequence(
                            key(),
                            keyStoreProp,
                            Optional(whitespace()),
                            FirstOf(eolChar(), EOI)
                    )
            ),
            actionStoreProp
    );
}
 
Example #5
Source File: MapExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
public Rule Expression () {
    StringExpressionParser stringExpressionParser = parserContext().parser(StringExpressionParser.class);
    NumberExpressionParser numberExpressionParser = parserContext().parser(NumberExpressionParser.class);
    VariableExpressionParser variableExpressionParser = parserContext().parser(VariableExpressionParser.class);
    return FirstOf(
            Sequence(
                    stringExpressionParser.ExpressionRule(),
                    push(stringExpressionParser.pop().getConstantValue().toString())
            ),
            Sequence(
                    numberExpressionParser.ExpressionRule(),
                    push(numberExpressionParser.pop().getConstantValue().toString())
            ),
            Sequence(
                    variableExpressionParser.ExpressionRule(),
                    push(variableExpressionParser.pop().getIdentifier())
            )
    );
}
 
Example #6
Source File: EmbedNodeParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
Rule Definitions() {
    OverrideBlockNodeParser blockNodeParser = parserContext().parser(OverrideBlockNodeParser.class);
    return Sequence(
            push(new ArrayList<OverrideBlockNode>()),

            ZeroOrMore(
                    FirstOf(
                            Sequence(
                                    blockNodeParser.NodeRule(),
                                    parserContext().parser(SpacingParser.class).Spacing(),
                                    peek(1).add(blockNodeParser.pop())
                            ),
                            Sequence(
                                    parserContext().parser(CommentParser.class).Comment(),
                                    parserContext().parser(SpacingParser.class).Spacing()
                            ),
                            invalidConstruct()
                    )
            )
    );
}
 
Example #7
Source File: FunctionExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Override
public Rule ExpressionRule() {
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);
    FunctionNameParser functionNameParser = parserContext().parser(FunctionNameParser.class);
    ArgumentsParser argumentsParser = parserContext().parser(ArgumentsParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),
            functionNameParser.Name(),
            argumentsParser.Arguments(), spacingParser.Spacing(),
            push(new FunctionExpression(
                    positionTrackerParser.pop(2),
                    functionNameParser.pop(1),
                    argumentsParser.pop()
            ))
    );
}
 
Example #8
Source File: OfficialLeelazAnalyzerV1.java    From mylizzie with GNU General Public License v3.0 6 votes vote down vote up
Rule EngineLine() {
    return Sequence(
            String("info"), pushInitialValueMap()
            , Spaces()
            , String("move")
            , Spaces()
            , Move(), saveMatchToValueMap("MOVE")
            , Spaces()
            , String("visits")
            , Spaces()
            , IntNumber(), saveMatchToValueMap("CALCULATION")
            , Spaces()
            , String("winrate")
            , Spaces()
            , DoubleNumber(), saveMatchToValueMap("VALUE")
            , Optional(
                    Spaces()
                    , FirstOf(String("network"), String("N"))
                    , Spaces()
                    , DoubleNumber(), saveMatchToValueMap("POLICY")
            )
            , Spaces()
            , String("pv"), saveAttrToValueMap("PV", Lists.mutable.empty())
            , ZeroOrMore(Sequence(Spaces(), Move(), pushMatchToList("PV")))
    );
}
 
Example #9
Source File: IfNodeParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
Rule elseCondition() {
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    LimitsParser limitsParser = parserContext().parser(LimitsParser.class);
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);
    LexicParser lexicParser = parserContext().parser(LexicParser.class);
    CompositeNodeParser compositeNodeParser = parserContext().parser(CompositeNodeParser.class);
    return Sequence(
        // Start
        Sequence(
            positionTrackerParser.PushPosition(),
            limitsParser.startCode(), spacingParser.Spacing(),
            lexicParser.Keyword(Keyword.ELSE),
            spacingParser.Spacing(),
            Mandatory(limitsParser.endCode(), "Expecting ending of else block")
        ),

        compositeNodeParser.NodeRule(),
        peek(2).add(new IfConditionNode(
            positionTrackerParser.pop(1),
            new ConstantExpression(positionTrackerParser.currentPosition(), true),
            compositeNodeParser.pop()
        ))
    );
}
 
Example #10
Source File: MacroNodeParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
Rule Parameters() {
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);
    VariableExpressionParser variableExpressionParser = parserContext().parser(VariableExpressionParser.class);
    return Sequence(
            push(new ArrayList<VariableExpression>()),

            String("("), spacingParser.Spacing(),
            Optional(
                    variableExpressionParser.ExpressionRule(),
                    spacingParser.Spacing(),
                    peek(1).add(variableExpressionParser.pop()),

                    ZeroOrMore(
                            String(","), spacingParser.Spacing(),
                            variableExpressionParser.ExpressionRule(),
                            spacingParser.Spacing(),
                            peek(1).add(variableExpressionParser.pop())
                    )
            ),
            String(")")
    );
}
 
Example #11
Source File: OutputNodeParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Override
public Rule NodeRule() {
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    LimitsParser limitsParser = parserContext().parser(LimitsParser.class);
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);
    AnyExpressionParser anyExpressionParser = parserContext().parser(AnyExpressionParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),

            limitsParser.startOutput(),
            spacingParser.Spacing(),
            Mandatory(anyExpressionParser.ExpressionRule(), "Missing or invalid output expression"),
            spacingParser.Spacing(),

            Mandatory(limitsParser.endOutput(), "Expecting end of output code island (Hint: Try to wrap parts of your expression in parentheses)"),

            push(new OutputNode(
                    positionTrackerParser.pop(1),
                    anyExpressionParser.pop()
            ))
    );
}
 
Example #12
Source File: TestOperationExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Override
@Label("Test Operation")
public Rule ExpressionRule() {
    SpacingParser spacingParser = parserContext().parser(SpacingParser.class);
    SimpleExpressionParser simpleExpressionParser = parserContext().parser(SimpleExpressionParser.class);
    AnyTestExpressionParser testExpressionParser = parserContext().parser(AnyTestExpressionParser.class);
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),
            simpleExpressionParser.ExpressionRule(),
            spacingParser.Spacing(),
            parserContext().parser(LexicParser.class).Keyword(Keyword.IS),
            spacingParser.Spacing(),
            testExpressionParser.Test(),

            push(new TestOperationExpression(positionTrackerParser.pop(2), simpleExpressionParser.pop(1), testExpressionParser.pop()))
    );
}
 
Example #13
Source File: BinaryOperationSuffixExpressionParser.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
Rule BinaryOperation(Rule expressionRule, List<BinaryOperator> operators) {
    return Sequence(
            expressionRule,
            parserContext().parser(SpacingParser.class).Spacing(),
            ZeroOrMore(
                    parserContext().parser(PositionTrackerParser.class).PushPosition(),
                    parserContext().parser(BinaryOperatorParser.class).BinaryOperator(operators),
                    parserContext().parser(SpacingParser.class).Spacing(),
                    Sequence(
                            parserContext().parser(PrimaryExpressionParser.class).ExpressionRule(),
                            expressionRule
                    ),
                    push(new BinaryOperationExpression(
                            parserContext().parser(PositionTrackerParser.class).pop(2),
                            pop(2),
                            parserContext().parser(BinaryOperatorParser.class).pop(1),
                            pop()
                    ))
            )

    );
}
 
Example #14
Source File: FunctionExpressionParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
public Rule Name () {
    return FirstOf(
            Sequence(
                    parserContext().parser(LexicParser.class).Keyword(Keyword.BLOCK),
                    push("block")
            ),
            Sequence(
                    parserContext().parser(LexicParser.class).Identifier(),
                    push(match())
            )
    );
}
 
Example #15
Source File: LexicParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
Rule[] keywordRules() {
    Keyword[] keywords = Keyword.values();
    keywordRules = new Rule[keywords.length + extraKeywords.size()];
    int i = 0;
    for (; i < keywords.length; i++) {
        keywordRules[i] = Keyword(keywords[i]);
    }
    for (String extraKeyword : extraKeywords) {
        keywordRules[i++] = Keyword(extraKeyword);
    }
    return keywordRules;
}
 
Example #16
Source File: LexicParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
public Rule Identifier() {
    Rule identifier = Sequence(
            TestNot(Keyword()),
            Letter()
    );
    if (keywordRules.length == 0) {
        identifier = Letter();
    }
    return Sequence(
            identifier,
            ZeroOrMore(LetterOrDigit())
    );
}
 
Example #17
Source File: BinaryOperationSuffixExpressionParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Override
@Label("BinaryOperationSuffix Expression")
public Rule ExpressionRule() {
    Rule initialExpression = EMPTY;
    ImmutableListMultimap<Integer, BinaryOperator> index = Multimaps.index(operators, precedence());

    List<Integer> integers = new ArrayList<>(index.keySet());
    Collections.sort(integers);
    for (Integer integer : integers) {
        initialExpression = BinaryOperation(initialExpression, index.get(integer));
    }
    return initialExpression;
}
 
Example #18
Source File: VerbatimNodeParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
Rule endVerbatim(LimitsParser limitsParser, SpacingParser spacingParser, LexicParser lexicParser) {
    return Sequence(
            limitsParser.startCode(),
            spacingParser.Spacing(),
            lexicParser.Keyword(Keyword.END_VERBATIM),
            spacingParser.Spacing(),
            limitsParser.endCode()
    );
}
 
Example #19
Source File: AnyTestExpressionParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
Rule[] rulesFor(TestExpressionParser[] parsers, NotParser notParser) {
    Rule[] rules = new Rule[parsers.length];
    for (int i = 0; i < parsers.length; i++) {
        rules[i] = ruleFor(parsers[i], notParser);
    }
    return rules;
}
 
Example #20
Source File: BooleanExpressionParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
public Rule FalseRule () {
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    LexicParser lexicParser = parserContext().parser(LexicParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),
            lexicParser.Keyword(Keyword.FALSE),
            push(new ConstantExpression(positionTrackerParser.pop(), false))
    );
}
 
Example #21
Source File: CfgPropsParboiled.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
Rule literal() {
    return OneOrMore(
            FirstOf(
                    new JavaIdPartMatcher(),
                    AnyOf("(){}-+*/^|;,`°§<>\"'%&@?"),
                    encodedSpecialChar(),
                    encodedTab(),
                    encodedLinefeed(),
                    encodedUnicode()
            )
    );
}
 
Example #22
Source File: SQLParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
Rule ColumnNameValue() {
  return Sequence(
      WhiteSpace(),
      TableAliasColname(),
      WhiteSpace(),
      FirstOf(Ch('='), IgnoreCase("IS")),
      WhiteSpace(),
      ColumnValue(),
      WhiteSpace()
  );
}
 
Example #23
Source File: TextNodeParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Override
public Rule NodeRule() {
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    TextBuilderParser textBuilderParser = parserContext().parser(TextBuilderParser.class);
    LimitsParser limitsParser = parserContext().parser(LimitsParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),
            textBuilderParser.Text(),
            push(new TextNode(
                    positionTrackerParser.pop(1),
                    textBuilderParser.pop().toString(),
                    new TextNode.Configuration().setTrimLeft(limitsParser.lastWhiteSpace()))),
            limitsParser.update(peek())
    );
}
 
Example #24
Source File: SQLParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
Rule Insert() {
  return Sequence(
      WhiteSpace(),
      IgnoreCase("INSERT"),
      WhiteSpace(),
      IgnoreCase("INTO"),
      WhiteSpace(),
      Schema(),
      Ch('.'),
      Table(),
      WhiteSpace(),
      Ch('('),
      WhiteSpace(),
      ColumnName(),
      ZeroOrMore(Sequence(WhiteSpace(), Ch(','), WhiteSpace(), ColumnName())),
      WhiteSpace(),
      Ch(')'),
      WhiteSpace(),
      IgnoreCase("VALUES"),
      WhiteSpace(),
      Ch('('),
      WhiteSpace(),
      ColumnValue(),
      ZeroOrMore(Sequence(WhiteSpace(), Ch(','), WhiteSpace(), ColumnValue())),
      Ch(')'),
      WhiteSpace()
  );
}
 
Example #25
Source File: CfgPropsParboiled.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
Rule encodedUnicode() {
    return Sequence(
            Ch('\\'),
            Ch('u'),
            Sequence(hexDigit(), hexDigit(), hexDigit(), hexDigit())
    );
}
 
Example #26
Source File: ExtendsNodeParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
Rule Definitions () {
    OverrideBlockNodeParser blockNodeParser = parserContext().parser(OverrideBlockNodeParser.class);
    SetNodeParser setNodeParser = parserContext().parser(SetNodeParser.class);
    ImportNodeParser importNodeParser = parserContext().parser(ImportNodeParser.class);
    return Sequence(
            push(new ArrayList<Node>()),
            ZeroOrMore(
                    FirstOf(
                            Sequence(
                                    blockNodeParser.NodeRule(),
                                    peek(1).add(blockNodeParser.pop())
                            ),
                            Sequence(
                                    setNodeParser.NodeRule(),
                                    peek(1).add(setNodeParser.pop())
                            ),
                            Sequence(
                                    importNodeParser.NodeRule(),
                                    peek(1).add(importNodeParser.pop())
                            ),
                            Sequence(
                                    parserContext().parser(CommentParser.class).Comment(),
                                    parserContext().parser(SpacingParser.class).Spacing()
                            ),
                            invalidConstruct()
                    ),
                    parserContext().parser(SpacingParser.class).Spacing()
            )
    );
}
 
Example #27
Source File: SQLParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
Rule TableAlias() {
  return ZeroOrMore(
      Sequence(
          WhiteSpace(),
          Sequence(TestNot(IgnoreCase("SET")), TestNot(IgnoreCase("WHERE")), TestNot('(')),
          OneOrMore(FirstOf(CharRange('A', 'Z'), CharRange('a', 'z'))),
          WhiteSpace()
      )
  );
}
 
Example #28
Source File: CfgPropsParboiled.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
Rule collectionIndex() {
    return Sequence(
            Ch('['),
            literal(),
            Ch(']')
    );
}
 
Example #29
Source File: NumberExpressionParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Override
public Rule ExpressionRule() {
    NumberParser numberParser = parserContext().parser(NumberParser.class);
    PositionTrackerParser positionTrackerParser = parserContext().parser(PositionTrackerParser.class);
    return Sequence(
            positionTrackerParser.PushPosition(),
            numberParser.NumberRule(),
            push(new ConstantExpression(positionTrackerParser.pop(1), numberParser.pop()))
    );
}
 
Example #30
Source File: BinaryOrPrimaryExpressionParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Override
public Rule ExpressionRule() {
    return FirstOf(
            parserContext().parser(BinaryOperationExpressionParser.class).ExpressionRule(),
            parserContext().parser(PrimaryExpressionParser.class).ExpressionRule()
    );
}