Exemplo n.º 1
0
        private RegexFactor VisitRegexFactorNode(IInternalTreeNode internalNode)
        {
            RegexAtom     atom     = null;
            RegexIterator?iterator = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var childInternalNode            = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.Atom == childInternalNodeSymbolValue)
                {
                    atom = VisitRegexAtomNode(childInternalNode);
                }
                else if (RegexGrammar.Iterator == childInternalNodeSymbolValue)
                {
                    iterator = VisitRegexIteratorNode(childInternalNode);
                }
            }

            if (iterator.HasValue)
            {
                return(new RegexFactorIterator(
                           atom,
                           iterator.Value));
            }

            return(new RegexFactor(atom));
        }
Exemplo n.º 2
0
        private EbnfBlockRule VisitRuleNode(IInternalTreeNode node)
        {
            EbnfQualifiedIdentifier qualifiedIdentifier = null;
            EbnfExpression          expression          = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;

                    if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                    {
                        qualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                    }
                    else if (EbnfGrammar.Expression == symbolValue)
                    {
                        expression = VisitExpressionNode(internalNode);
                    }
                    break;

                case TreeNodeType.Token:
                    break;
                }
            }
            return(new EbnfBlockRule(
                       new EbnfRule(qualifiedIdentifier, expression)));
        }
Exemplo n.º 3
0
        private RegexExpression VisitRegexExpressionNode(IInternalTreeNode node)
        {
            RegexExpression expression = null;
            IRegexTerm      term       = null;

            foreach (var internalNode in node.Children.OfType <IInternalTreeNode>())
            {
                if (internalNode.Is(RegexGrammar.Expression))
                {
                    expression = VisitRegexExpressionNode(internalNode);
                }
                else if (internalNode.Is(RegexGrammar.Term))
                {
                    term = VisitRegexTermNode(internalNode);
                }
            }

            if (expression != null && term != null)
            {
                return(new RegexExpressionAlteration(term, expression));
            }

            if (term != null)
            {
                return(new RegexExpressionTerm(term));
            }

            throw new InvalidOperationException("Unable to create null expression.");
        }
Exemplo n.º 4
0
 public override void Visit(IInternalTreeNode node)
 {
     if (node.Is(RegexGrammar.Regex))
     {
         Regex = VisitRegexNode(node);
     }
 }
Exemplo n.º 5
0
        private static RegexCharacters VisitCharacterRangeNode(IInternalTreeNode node)
        {
            RegexCharacterClassCharacter start = null;
            RegexCharacterClassCharacter end   = null;

            foreach (var internalNode in node.Children.OfType <IInternalTreeNode>())
            {
                if (internalNode.Is(RegexGrammar.CharacterClassCharacter))
                {
                    if (start == null)
                    {
                        start = VisitCharacterClassCharacterNode(internalNode);
                    }
                    else
                    {
                        end = VisitCharacterClassCharacterNode(internalNode);
                    }
                }
            }

            if (end == null)
            {
                return(new RegexCharactersUnit(start));
            }

            return(new RegexCharactersRange(start, end));
        }
Exemplo n.º 6
0
        private EbnfBlockSetting VisitSettingNode(IInternalTreeNode node)
        {
            EbnfSettingIdentifier   settingIdentifier = null;
            EbnfQualifiedIdentifier qualifiedEbnfQualifiedIdentifier = null;

            foreach (var child in node.Children)
            {
                switch (child)
                {
                case IInternalTreeNode internalNode:
                    if (internalNode.Is(EbnfGrammar.QualifiedIdentifier))
                    {
                        qualifiedEbnfQualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                    }

                    break;

                case ITokenTreeNode tokenNode:
                    var token = tokenNode.Token;
                    if (token.TokenName.Equals(EbnfGrammar.TokenClasses.SettingIdentifier))
                    {
                        settingIdentifier = new EbnfSettingIdentifier(token.Value);
                    }

                    break;
                }
            }

            return(new EbnfBlockSetting(new EbnfSetting(settingIdentifier, qualifiedEbnfQualifiedIdentifier)));
        }
Exemplo n.º 7
0
        private RegexSet VisitRegexSetNode(IInternalTreeNode internalNode)
        {
            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }

                var childInternalNode            = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.PositiveSet == childInternalNodeSymbolValue)
                {
                    return(VisitInnerSetNode(false, childInternalNode));
                }

                else if (RegexGrammar.NegativeSet == childInternalNodeSymbolValue)
                {
                    return(VisitInnerSetNode(true, childInternalNode));
                }
            }
            throw new Exception("Invalid Set Detected.");
        }
Exemplo n.º 8
0
        private static ICapture <char> VisitLiteralNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Token:
                    var tokenNode = child as ITokenTreeNode;
                    var token     = tokenNode.Token;
                    var tokenType = token.TokenType;

                    // if token type is string token type remove surrounding quotes
                    if (tokenType.Equals(SingleQuoteStringLexerRule.TokenTypeDescriptor) ||
                        tokenType.Equals(DoubleQuoteStringLexerRule.TokenTypeDescriptor))
                    {
                        return(token.Capture.Slice(1, token.Capture.Count - 2));
                    }

                    // TODO: Find a better solution for identifing the lexer rule based on id
                    if (tokenNode.Token.TokenType.Id.Length > 5)
                    {
                        return(token.Capture);
                    }

                    break;

                case TreeNodeType.Internal:
                    break;
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 9
0
        private EbnfDefinition VisitDefinitionNode(IInternalTreeNode node)
        {
            EbnfBlock      block      = null;
            EbnfDefinition definition = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;

                    if (EbnfGrammar.Block == symbolValue)
                    {
                        block = VisitBlockNode(internalNode);
                    }
                    else if (EbnfGrammar.Definition == symbolValue)
                    {
                        definition = VisitDefinitionNode(internalNode);
                    }
                    break;
                }
            }

            if (definition != null)
            {
                return(new EbnfDefinitionConcatenation(block, definition));
            }

            return(new EbnfDefinition(block));
        }
Exemplo n.º 10
0
 public override void Visit(IInternalTreeNode node)
 {
     if (RegexGrammar.Regex == node.Symbol.Value)
     {
         Regex = VisitRegexNode(node);
     }
 }
Exemplo n.º 11
0
 public override void Visit(IInternalTreeNode node)
 {
     if (EbnfGrammar.Definition == node.Symbol.Value)
     {
         Definition = VisitDefinitionNode(node);
     }
 }
Exemplo n.º 12
0
        private RegexCharacterClass VisitCharacterClassNode(IInternalTreeNode internalNode)
        {
            RegexCharacterUnitRange characterRange = null;
            RegexCharacterClass characterClass = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.CharacterRange == childInternalNodeSymbolValue)
                    characterRange = VisitCharacterRangeNode(childInternalNode);

                else if (RegexGrammar.CharacterClass == childInternalNodeSymbolValue)
                    characterClass = VisitCharacterClassNode(childInternalNode);

            }
            if (characterClass != null)
                return new RegexCharacterClassAlteration(
                    characterRange,
                    characterClass);

            return new RegexCharacterClass(characterRange);
        }
Exemplo n.º 13
0
 public virtual void Visit(IInternalTreeNode node)
 {
     foreach (var child in node.Children)
     {
         child.Accept(this);
     }
 }
Exemplo n.º 14
0
        private EbnfExpression VisitExpressionNode(IInternalTreeNode node)
        {
            EbnfTerm       term       = null;
            EbnfExpression expression = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;
                    if (EbnfGrammar.Term == symbolValue)
                    {
                        term = VisitTermNode(internalNode);
                    }
                    else if (EbnfGrammar.Expression == symbolValue)
                    {
                        expression = VisitExpressionNode(internalNode);
                    }
                    break;

                case TreeNodeType.Token:
                    break;
                }
            }
            if (expression == null)
            {
                return(new EbnfExpression(term));
            }
            return(new EbnfExpressionAlteration(term, expression));
        }
Exemplo n.º 15
0
        private EbnfBlockSetting VisitSettingNode(IInternalTreeNode node)
        {
            EbnfSettingIdentifier   settingIdentifier   = null;
            EbnfQualifiedIdentifier qualifiedIdentifier = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Token:
                    var tokenNode = child as ITokenTreeNode;
                    var token     = tokenNode.Token;
                    if (token.TokenType.Id == nameof(settingIdentifier))
                    {
                        settingIdentifier = new EbnfSettingIdentifier(token.Value);
                    }
                    break;

                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;
                    if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                    {
                        qualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                    }
                    break;
                }
            }
            return(new EbnfBlockSetting(
                       new EbnfSetting(settingIdentifier, qualifiedIdentifier)));
        }
Exemplo n.º 16
0
 public override void Visit(IInternalTreeNode node)
 {
     if (node.Is(EbnfGrammar.Definition))
     {
         Definition = VisitDefinitionNode(node);
     }
 }
Exemplo n.º 17
0
        private EbnfLexerRuleExpression VisitLexerRuleExpressionNode(IInternalTreeNode node)
        {
            EbnfLexerRuleTerm       term       = null;
            EbnfLexerRuleExpression expression = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var internalNode = child as IInternalTreeNode;
                var symbolValue  = internalNode.Symbol.Value;
                if (EbnfGrammar.LexerRuleTerm == symbolValue)
                {
                    term = VisitLexerRuleTermNode(internalNode);
                }
                if (EbnfGrammar.LexerRuleExpression == symbolValue)
                {
                    expression = VisitLexerRuleExpressionNode(internalNode);
                }
            }

            if (expression == null)
            {
                return(new EbnfLexerRuleExpression(term));
            }
            return(new EbnfLexerRuleExpressionAlteration(term, expression));
        }
Exemplo n.º 18
0
        private EbnfBlockLexerRule VisitLexerRuleNode(IInternalTreeNode node)
        {
            EbnfQualifiedIdentifier qualifiedIdentifier = null;
            EbnfLexerRuleExpression expression          = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var internalNode = child as IInternalTreeNode;
                var symbolValue  = internalNode.Symbol.Value;
                if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                {
                    qualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                }
                else if (EbnfGrammar.LexerRuleExpression == symbolValue)
                {
                    expression = VisitLexerRuleExpressionNode(internalNode);
                }
            }
            return(new EbnfBlockLexerRule(
                       new EbnfLexerRule(qualifiedIdentifier, expression)));
        }
Exemplo n.º 19
0
        private EbnfLexerRuleTerm VisitLexerRuleTermNode(IInternalTreeNode node)
        {
            EbnfLexerRuleFactor factor = null;
            EbnfLexerRuleTerm   term   = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var internalNode = child as IInternalTreeNode;
                var symbolValue  = internalNode.Symbol.Value;
                if (EbnfGrammar.LexerRuleFactor == symbolValue)
                {
                    factor = VisitLexerRuleFactorNode(internalNode);
                }
                if (EbnfGrammar.LexerRuleTerm == symbolValue)
                {
                    term = VisitLexerRuleTermNode(internalNode);
                }
            }

            if (term == null)
            {
                return(new EbnfLexerRuleTerm(factor));
            }

            return(new EbnfLexerRuleTermConcatenation(factor, term));
        }
Exemplo n.º 20
0
        private static EbnfLexerRuleFactor VisitLexerRuleFactorNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var internalNode = child as IInternalTreeNode;
                var symbolValue  = internalNode.Symbol.Value;

                if (EbnfGrammar.Literal == symbolValue)
                {
                    return(new EbnfLexerRuleFactorLiteral(
                               VisitLiteralNode(internalNode)));
                }

                if (RegexGrammar.Regex == symbolValue)
                {
                    var regexVisitor = new RegexVisitor();
                    internalNode.Accept(regexVisitor);
                    return(new EbnfLexerRuleFactorRegex(regexVisitor.Regex));
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 21
0
        private EbnfBlock VisitBlockNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;

                    if (EbnfGrammar.Rule == symbolValue)
                    {
                        return(VisitRuleNode(internalNode));
                    }
                    if (EbnfGrammar.Setting == symbolValue)
                    {
                        return(VisitSettingNode(internalNode));
                    }
                    if (EbnfGrammar.LexerRule == symbolValue)
                    {
                        return(VisitLexerRuleNode(internalNode));
                    }
                    break;
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 22
0
        private static RegexCharacterUnitRange VisitCharacterRangeNode(IInternalTreeNode internalNode)
        {
            RegexCharacterClassCharacter start = null;
            RegexCharacterClassCharacter end = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;

                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;
                if (RegexGrammar.CharacterClassCharacter == childInternalNodeSymbolValue)
                {
                    if (start == null)
                        start = VisitCharacterClassCharacterNode(childInternalNode);
                    else
                        end = VisitCharacterClassCharacterNode(childInternalNode);
                }
            }
            if (end != null)
                return new RegexCharacterRange(start, end);

            return new RegexCharacterUnitRange(start);
        }
Exemplo n.º 23
0
        private EbnfTerm VisitTermNode(IInternalTreeNode node)
        {
            EbnfFactor factor = null;
            EbnfTerm   term   = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;
                    if (EbnfGrammar.Factor == symbolValue)
                    {
                        factor = VisitFactorNode(internalNode);
                    }
                    else if (EbnfGrammar.Term == symbolValue)
                    {
                        term = VisitTermNode(internalNode);
                    }
                    break;

                case TreeNodeType.Token:
                    break;
                }
            }
            if (term == null)
            {
                return(new EbnfTerm(factor));
            }
            return(new EbnfTermConcatenation(factor, term));
        }
Exemplo n.º 24
0
        private RegexTerm VisitRegexTermNode(IInternalTreeNode internalNode)
        {
            RegexFactor factor = null;
            RegexTerm   term   = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var childInternalNode            = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.Factor == childInternalNodeSymbolValue)
                {
                    factor = VisitRegexFactorNode(childInternalNode);
                }
                else if (RegexGrammar.Term == childInternalNodeSymbolValue)
                {
                    term = VisitRegexTermNode(childInternalNode);
                }
            }
            if (term == null)
            {
                return(new RegexTerm(factor));
            }

            return(new RegexTermFactor(factor, term));
        }
Exemplo n.º 25
0
        private IEnumerable <string> VisitQualifiedIdentifier(IInternalTreeNode node)
        {
            foreach (var child in node.Children)
            {
                switch (child)
                {
                case IInternalTreeNode internalNode:
                    if (internalNode.Is(EbnfGrammar.QualifiedIdentifier))
                    {
                        foreach (var identifier in VisitQualifiedIdentifier(internalNode))
                        {
                            yield return(identifier);
                        }
                    }
                    break;

                case ITokenTreeNode tokenNode:
                    var token = tokenNode.Token;
                    if (token.TokenName.Equals(EbnfGrammar.TokenClasses.Identifier))
                    {
                        yield return(token.Value);
                    }
                    break;
                }
            }
        }
Exemplo n.º 26
0
        private static RegexCharacterUnitRange VisitCharacterRangeNode(IInternalTreeNode internalNode)
        {
            RegexCharacterClassCharacter start = null;
            RegexCharacterClassCharacter end   = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }

                var childInternalNode            = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;
                if (RegexGrammar.CharacterClassCharacter == childInternalNodeSymbolValue)
                {
                    if (start == null)
                    {
                        start = VisitCharacterClassCharacterNode(childInternalNode);
                    }
                    else
                    {
                        end = VisitCharacterClassCharacterNode(childInternalNode);
                    }
                }
            }
            if (end != null)
            {
                return(new RegexCharacterRange(start, end));
            }

            return(new RegexCharacterUnitRange(start));
        }
Exemplo n.º 27
0
        private RegexCharacterClass VisitCharacterClassNode(IInternalTreeNode internalNode)
        {
            RegexCharacterUnitRange characterRange = null;
            RegexCharacterClass     characterClass = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var childInternalNode            = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.CharacterRange == childInternalNodeSymbolValue)
                {
                    characterRange = VisitCharacterRangeNode(childInternalNode);
                }

                else if (RegexGrammar.CharacterClass == childInternalNodeSymbolValue)
                {
                    characterClass = VisitCharacterClassNode(childInternalNode);
                }
            }
            if (characterClass != null)
            {
                return(new RegexCharacterClassAlteration(
                           characterRange,
                           characterClass));
            }

            return(new RegexCharacterClass(characterRange));
        }
Exemplo n.º 28
0
        public virtual void Visit(IInternalTreeNode node)
        {
            var nodeChildrenCount = node.Children.Count;

            for (var i = 0; i < nodeChildrenCount; i++)
            {
                node.Children[i].Accept(this);
            }
        }
Exemplo n.º 29
0
        public void Visit(IInternalTreeNode node)
        {
            this.writer.WriteLine($"{this.indent}{node}");
            this.indent += "   ";
            foreach (var child in node.Children)
            {
                child.Accept(this);
            }

            this.indent = this.indent.Substring(0, this.indent.Length - 3);
        }
Exemplo n.º 30
0
        private EbnfFactorOptional VisitOptionalNode(IInternalTreeNode node)
        {
            foreach (var internalNode in node.Children.OfType <IInternalTreeNode>())
            {
                if (internalNode.Is(EbnfGrammar.Expression))
                {
                    return(new EbnfFactorOptional(VisitExpressionNode(internalNode)));
                }
            }

            throw UnreachableCodeException();
        }
Exemplo n.º 31
0
        private EbnfFactor VisitFactorNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;

                    if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                    {
                        return(new EbnfFactorIdentifier(
                                   VisitQualifiedIdentifierNode(internalNode)));
                    }

                    if (EbnfGrammar.Literal == symbolValue)
                    {
                        return(new EbnfFactorLiteral(
                                   VisitLiteralNode(internalNode)));
                    }

                    if (EbnfGrammar.Repetition == symbolValue)
                    {
                        return(VisitRepetitionNode(internalNode));
                    }

                    if (EbnfGrammar.Optional == symbolValue)
                    {
                        return(VisitOptionalNode(internalNode));
                    }

                    if (EbnfGrammar.Grouping == symbolValue)
                    {
                        return(VisitGroupingNode(internalNode));
                    }

                    if (RegexGrammar.Regex == symbolValue)
                    {
                        var regexVisitor = new RegexVisitor();
                        internalNode.Accept(regexVisitor);
                        return(new EbnfFactorRegex(regexVisitor.Regex));
                    }
                    break;

                case TreeNodeType.Token:
                    break;
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 32
0
        private RegexSet VisitInnerSetNode(bool negate, IInternalTreeNode node)
        {
            foreach (var internalNode in node.Children.OfType <IInternalTreeNode>())
            {
                if (internalNode.Is(RegexGrammar.CharacterClass))
                {
                    var characterClass = VisitCharacterClassNode(internalNode);
                    return(new RegexSet(characterClass, negate));
                }
            }

            throw new Exception("Invalid Inner Set Detected");
        }
Exemplo n.º 33
0
        private static RegexCharacterClassCharacter VisitCharacterClassCharacterNode(IInternalTreeNode internalNode)
        {
            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Token)
                    continue;

                var childTokenNode = child as ITokenTreeNode;
                var value = childTokenNode.Token.Value.StartsWith(@"\")
                    ? childTokenNode.Token.Value[1]
                    : childTokenNode.Token.Value[0];
                return new RegexCharacterClassCharacter(value);
            }
            throw new Exception("Invalid Regex Character Class Character.");
        }
Exemplo n.º 34
0
        private static string VisitLiteralNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Token:
                        var tokenNode = child as ITokenTreeNode;
                        // TODO: Find a better solution for identifing the lexer rule based on id
                        if (tokenNode.Token.TokenType.Id.Length > 5)
                            return tokenNode.Token.Value;
                        break;

                    case TreeNodeType.Internal:
                        break;
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 35
0
        private EbnfBlock VisitBlockNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;

                        if (EbnfGrammar.Rule == symbolValue)
                            return VisitRuleNode(internalNode);
                        if (EbnfGrammar.Setting == symbolValue)
                            return VisitSettingNode(internalNode);
                        if (EbnfGrammar.LexerRule == symbolValue)
                            return VisitLexerRuleNode(internalNode);
                        break;
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 36
0
        private static EbnfLexerRuleFactor VisitLexerRuleFactorNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var internalNode = child as IInternalTreeNode;
                var symbolValue = internalNode.Symbol.Value;

                if (EbnfGrammar.Literal == symbolValue)
                    return new EbnfLexerRuleFactorLiteral(
                        VisitLiteralNode(internalNode));

                if (RegexGrammar.Regex == symbolValue)
                {
                    var regexVisitor = new RegexVisitor();
                    internalNode.Accept(regexVisitor);
                    return new EbnfLexerRuleFactorRegex(regexVisitor.Regex);
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 37
0
 private EbnfFactorRepetition VisitRepetitionNode(IInternalTreeNode node)
 {
     for (int c = 0; c < node.Children.Count; c++)
     {
         var child = node.Children[c];
         switch (child.NodeType)
         {
             case TreeNodeType.Internal:
                 var internalNode = child as IInternalTreeNode;
                 var symbolValue = internalNode.Symbol.Value;
                 if (EbnfGrammar.Expression == symbolValue)
                 {
                     return new EbnfFactorRepetition(VisitExpressionNode(internalNode));
                 }
                 break;
         }
     }
     throw UnreachableCodeException();
 }
Exemplo n.º 38
0
 private RegexSet VisitInnerSetNode(bool negate, IInternalTreeNode internalNode)
 {
     for (var c = 0; c < internalNode.Children.Count; c++)
     {
         var child = internalNode.Children[c];
         if (child.NodeType != TreeNodeType.Internal)
             continue;
         var childInternalNode = child as IInternalTreeNode;
         var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;
         if (RegexGrammar.CharacterClass == childInternalNodeSymbolValue)
         {
             var characterClass = VisitCharacterClassNode(childInternalNode);
             return new RegexSet(negate, characterClass);
         }
     }
     throw new Exception("Invalid Inner Set Detected");
 }
Exemplo n.º 39
0
 public virtual void Visit(IInternalTreeNode node)
 {
     foreach (var child in node.Children)
         child.Accept(this);
 }
Exemplo n.º 40
0
        private EbnfFactor VisitFactorNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;

                        if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                            return new EbnfFactorIdentifier(
                                VisitQualifiedIdentifierNode(internalNode));

                        if (EbnfGrammar.Literal == symbolValue)
                            return new EbnfFactorLiteral(
                                VisitLiteralNode(internalNode));

                        if (EbnfGrammar.Repetition == symbolValue)
                            return VisitRepetitionNode(internalNode);

                        if (EbnfGrammar.Optional == symbolValue)
                            return VisitOptionalNode(internalNode);

                        if (EbnfGrammar.Grouping == symbolValue)
                            return VisitGroupingNode(internalNode);

                        if (RegexGrammar.Regex == symbolValue)
                        {
                            var regexVisitor = new RegexVisitor();
                            internalNode.Accept(regexVisitor);
                            return new EbnfFactorRegex(regexVisitor.Regex);
                        }
                        break;

                    case TreeNodeType.Token:
                        break;
                }
            }
            throw UnreachableCodeException();
        }
Exemplo n.º 41
0
        private EbnfLexerRuleExpression VisitLexerRuleExpressionNode(IInternalTreeNode node)
        {
            EbnfLexerRuleTerm term = null;
            EbnfLexerRuleExpression expression = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var internalNode = child as IInternalTreeNode;
                var symbolValue = internalNode.Symbol.Value;
                if (EbnfGrammar.LexerRuleTerm == symbolValue)
                    term = VisitLexerRuleTermNode(internalNode);
                if (EbnfGrammar.LexerRuleExpression == symbolValue)
                    expression = VisitLexerRuleExpressionNode(internalNode);
            }

            if (expression == null)
                return new EbnfLexerRuleExpression(term);
            return new EbnfLexerRuleExpressionAlteration(term, expression);
        }
Exemplo n.º 42
0
        private RegexIterator VisitRegexIteratorNode(IInternalTreeNode internalNode)
        {
            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Token)
                    continue;
                var tokenChildNode = child as ITokenTreeNode;
                switch (tokenChildNode.Token.Value)
                {
                    case "*":
                        return RegexIterator.ZeroOrMany;

                    case "?":
                        return RegexIterator.ZeroOrOne;

                    case "+":
                        return RegexIterator.OneOrMany;
                }
            }
            throw new Exception("Invalid iterator detected.");
        }
Exemplo n.º 43
0
        private EbnfQualifiedIdentifier VisitQualifiedIdentifierNode(IInternalTreeNode node)
        {
            EbnfQualifiedIdentifier repetitionIdentifier = null;
            string identifier = null;
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;
                        if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                            repetitionIdentifier = VisitQualifiedIdentifierNode(internalNode);
                        break;

                    case TreeNodeType.Token:
                        var tokenNode = child as ITokenTreeNode;
                        var token = tokenNode.Token;
                        if (token.TokenType.Id == "identifier")
                            identifier = token.Value;
                        break;
                }
            }
            if (repetitionIdentifier == null)
                return new EbnfQualifiedIdentifier(identifier);

            return new EbnfQualifiedIdentifierConcatenation(identifier, repetitionIdentifier);
        }
Exemplo n.º 44
0
        private RegexFactor VisitRegexFactorNode(IInternalTreeNode internalNode)
        {
            RegexAtom atom = null;
            RegexIterator? iterator = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.Atom == childInternalNodeSymbolValue)
                    atom = VisitRegexAtomNode(childInternalNode);
                else if(RegexGrammar.Iterator == childInternalNodeSymbolValue)
                    iterator = VisitRegexIteratorNode(childInternalNode);
            }

            if (iterator.HasValue)
                return new RegexFactorIterator(
                    atom,
                    iterator.Value);

            return new RegexFactor(atom);
        }
Exemplo n.º 45
0
 public override void Visit(IInternalTreeNode node)
 {
     if (RegexGrammar.Regex == node.Symbol.Value)
         Regex = VisitRegexNode(node);
 }
Exemplo n.º 46
0
        private RegexExpression VisitRegexExpressionNode(IInternalTreeNode internalNode)
        {
            RegexExpression expression = null;
            RegexTerm term = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.Expression == childInternalNodeSymbolValue)
                    expression = VisitRegexExpressionNode(childInternalNode);

                else if (RegexGrammar.Term == childInternalNodeSymbolValue)
                    term = VisitRegexTermNode(childInternalNode);
            }

            if (expression != null && term != null)
                return new RegexExpressionAlteration(term, expression);

            if (term != null)
                return new RegexExpressionTerm(term);

            throw new InvalidOperationException("Unable to create null expression.");
        }
Exemplo n.º 47
0
        private RegexAtom VisitRegexAtomNode(IInternalTreeNode internalNode)
        {
            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    var childTokenNode = child as ITokenTreeNode;
                    switch (childTokenNode.Token.Value)
                    {
                        case ".":
                            return new RegexAtomAny();

                        default:
                            continue;
                    }
                }

                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;
                if (RegexGrammar.Character == childInternalNodeSymbolValue)
                {
                    var character = VisitRegexCharacterNode(childInternalNode);
                    return new RegexAtomCharacter(character);
                }
                else if (RegexGrammar.Expression == childInternalNodeSymbolValue)
                {
                    var expression = VisitRegexExpressionNode(childInternalNode);
                    return new RegexAtomExpression(expression);
                }
                else if(RegexGrammar.Set == childInternalNodeSymbolValue)
                {
                    var set = VisitRegexSetNode(childInternalNode);
                    return new RegexAtomSet(set);
                }
            }
            throw new Exception("Unable to parse atom. Invalid child production.");
        }
Exemplo n.º 48
0
        private EbnfTerm VisitTermNode(IInternalTreeNode node)
        {
            EbnfFactor factor = null;
            EbnfTerm term = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;
                        if (EbnfGrammar.Factor == symbolValue)
                            factor = VisitFactorNode(internalNode);
                        else if (EbnfGrammar.Term == symbolValue)
                            term = VisitTermNode(internalNode);
                        break;

                    case TreeNodeType.Token:
                        break;
                }
            }
            if (term == null)
                return new EbnfTerm(factor);
            return new EbnfTermConcatenation(factor, term);
        }
Exemplo n.º 49
0
        private EbnfLexerRuleTerm VisitLexerRuleTermNode(IInternalTreeNode node)
        {
            EbnfLexerRuleFactor factor = null;
            EbnfLexerRuleTerm term = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var internalNode = child as IInternalTreeNode;
                var symbolValue = internalNode.Symbol.Value;
                if (EbnfGrammar.LexerRuleFactor == symbolValue)
                    factor = VisitLexerRuleFactorNode(internalNode);
                if (EbnfGrammar.LexerRuleTerm == symbolValue)
                    term = VisitLexerRuleTermNode(internalNode);
            }

            if (term == null)
                return new EbnfLexerRuleTerm(factor);

            return new EbnfLexerRuleTermConcatenation(factor, term);
        }
Exemplo n.º 50
0
        private Regex VisitRegexNode(IInternalTreeNode node)
        {
            RegexExpression expression = null;
            var startsWith = false;
            var endsWith = false;

            for (var c = 0; c< node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (node.NodeType)
                {
                    case TreeNodeType.Internal:

                        var internalNode = child as IInternalTreeNode;
                        var internalNodeSymbolValue = internalNode.Symbol.Value;
                        if(RegexGrammar.Expression == internalNodeSymbolValue)
                            expression = VisitRegexExpressionNode(internalNode);

                        break;

                    case TreeNodeType.Token:
                        var tokenNode = child as ITokenTreeNode;
                        switch (tokenNode.Token.Value)
                        {
                            case "$":
                                endsWith = true;
                                break;

                            case "^":
                                startsWith = true;
                                break;
                        }
                        break;
                }
            }
            return new Regex(startsWith, expression, endsWith);
        }
Exemplo n.º 51
0
        private EbnfBlockLexerRule VisitLexerRuleNode(IInternalTreeNode node)
        {
            EbnfQualifiedIdentifier qualifiedIdentifier = null;
            EbnfLexerRuleExpression expression = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var internalNode = child as IInternalTreeNode;
                var symbolValue = internalNode.Symbol.Value;
                if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                    qualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                else if (EbnfGrammar.LexerRuleExpression == symbolValue)
                    expression = VisitLexerRuleExpressionNode(internalNode);

            }
            return new EbnfBlockLexerRule(
                new EbnfLexerRule(qualifiedIdentifier, expression));
        }
Exemplo n.º 52
0
        private RegexSet VisitRegexSetNode(IInternalTreeNode internalNode)
        {
            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;

                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if(RegexGrammar.PositiveSet == childInternalNodeSymbolValue)
                    return VisitInnerSetNode(false, childInternalNode);

                else if(RegexGrammar.NegativeSet == childInternalNodeSymbolValue)
                    return VisitInnerSetNode(true, childInternalNode);
            }
            throw new Exception("Invalid Set Detected.");
        }
Exemplo n.º 53
0
 public override void Visit(IInternalTreeNode node)
 {
     if (EbnfGrammar.Definition == node.Symbol.Value)
         Definition = VisitDefinitionNode(node);
 }
Exemplo n.º 54
0
        private RegexTerm VisitRegexTermNode(IInternalTreeNode internalNode)
        {
            RegexFactor factor = null;
            RegexTerm term = null;
            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var childInternalNode = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.Factor == childInternalNodeSymbolValue)
                    factor = VisitRegexFactorNode(childInternalNode);
                else if (RegexGrammar.Term == childInternalNodeSymbolValue)
                    term = VisitRegexTermNode(childInternalNode);
            }
            if (term == null)
                return new RegexTerm(factor);

            return new RegexTermFactor(factor, term);
        }
Exemplo n.º 55
0
        private EbnfExpression VisitExpressionNode(IInternalTreeNode node)
        {
            EbnfTerm term = null;
            EbnfExpression expression = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;
                        if (EbnfGrammar.Term == symbolValue)
                            term = VisitTermNode(internalNode);
                        else if (EbnfGrammar.Expression == symbolValue)
                            expression = VisitExpressionNode(internalNode);
                        break;

                    case TreeNodeType.Token:
                        break;
                }
            }
            if (expression == null)
                return new EbnfExpression(term);
            return new EbnfExpressionAlteration(term, expression);
        }
Exemplo n.º 56
0
        private EbnfBlockSetting VisitSettingNode(IInternalTreeNode node)
        {
            EbnfSettingIdentifier settingIdentifier = null;
            EbnfQualifiedIdentifier qualifiedIdentifier = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Token:
                        var tokenNode = child as ITokenTreeNode;
                        var token = tokenNode.Token;
                        if (token.TokenType.Id == nameof(settingIdentifier))
                            settingIdentifier = new EbnfSettingIdentifier(token.Value);
                        break;

                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;
                        if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                            qualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                        break;
                }
            }
            return new EbnfBlockSetting(
                new EbnfSetting(settingIdentifier, qualifiedIdentifier));
        }
Exemplo n.º 57
0
        private EbnfBlockRule VisitRuleNode(IInternalTreeNode node)
        {
            EbnfQualifiedIdentifier qualifiedIdentifier = null;
            EbnfExpression expression = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;

                        if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                            qualifiedIdentifier = VisitQualifiedIdentifierNode(internalNode);
                        else if (EbnfGrammar.Expression == symbolValue)
                            expression = VisitExpressionNode(internalNode);
                        break;

                    case TreeNodeType.Token:
                        break;
                }
            }
            return new EbnfBlockRule(
                new EbnfRule(qualifiedIdentifier, expression));
        }
Exemplo n.º 58
0
        private EbnfDefinition VisitDefinitionNode(IInternalTreeNode node)
        {
            EbnfBlock block = null;
            EbnfDefinition definition = null;

            for (int c = 0; c< node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;

                        if (EbnfGrammar.Block == symbolValue)
                            block = VisitBlockNode(internalNode);
                        else if (EbnfGrammar.Definition == symbolValue)
                            definition = VisitDefinitionNode(internalNode);
                        break;
                }
            }

            if (definition != null)
                return new EbnfDefinitionConcatenation(block, definition);

            return new EbnfDefinition(block);
        }