Пример #1
0
        private void BuildParser2(Grammar grammar, Element element, ref string ret)
        {
            PatternIdentifier patternIdentifier = element as PatternIdentifier;
            DefIdentifier defIdentifier = element as DefIdentifier;
            AllOf allOf = element as AllOf;
            OneOf oneOf = element as OneOf;
            Optional optional = element as Optional;

            if (patternIdentifier != null)
            {
                ret += $@"
                Consume(child, TokenType.{patternIdentifier.Name.ToIdentifier()}, NodeType.{patternIdentifier.Name.ToIdentifier()});";
            }
            else if (defIdentifier != null)
            {
                ret += $@"
                {defIdentifier.Name}(child);";
            }
            else if (oneOf != null)
            {
                if (oneOf.Elements.Count == 1)
                {
                    patternIdentifier = oneOf.Elements[0] as PatternIdentifier;
                    defIdentifier = oneOf.Elements[0] as DefIdentifier;

                    if (patternIdentifier != null)
                    {
                        ret += $@"
                Consume(child, TokenType.{patternIdentifier.Name.ToIdentifier()}, NodeType.{patternIdentifier.Name.ToIdentifier()});";
                    }
                    else if (defIdentifier != null)
                    {
                        ret += $@"
                {defIdentifier.Name}(child);";
                    }
                }
                else
                {
                    bool first = true;

                    foreach (var identifier in oneOf.Elements)
                    {
                        patternIdentifier = identifier as PatternIdentifier;

                        ret += $@"
                {(first ? "" : "else ")}if ({GetAreTokenTypes(grammar, identifier)})
                {{";

                        if (patternIdentifier != null)
                        {
                            ret += $@"
                    Consume(child, TokenType.{patternIdentifier.Name.ToIdentifier()}, NodeType.{patternIdentifier.Name.ToIdentifier()});";
                        }
                        else
                        {
                            ret += $@"
                    {identifier.Name}(child);";
                        }

                        ret += $@"
                }}";

                        first = false;
                    }
                }
            }
            else if (allOf != null)
            {
                foreach (var identifier in allOf.Elements)
                {
                    patternIdentifier = identifier as PatternIdentifier;

                    if (patternIdentifier != null)
                    {
                        ret += $@"
                Consume(child, TokenType.{patternIdentifier.Name.ToIdentifier()}, NodeType.{patternIdentifier.Name.ToIdentifier()});";
                    }
                    else
                    {
                        ret += $@"
                {identifier.Name}(child);";
                    }
                }
            }
            else if (optional != null)
            {
                BuildParser2(grammar, optional.Element, ref ret);
            }
            else
            {
                throw new Exception();
            }
        }
Пример #2
0
 private List<string> GetTokenTypes(Grammar grammar, Element element)
 {
     List<string> tokenTypes = new List<string>();
     GetTokenTypes(grammar, element, ref tokenTypes);
     return tokenTypes;
 }
Пример #3
0
        private void GetTokenTypes(Grammar grammar, Element element, ref List<string> tokenTypes)
        {
            PatternIdentifier patternIdentifier = element as PatternIdentifier;
            DefIdentifier defIdentifier = element as DefIdentifier;
            //AllOf allOf = element as AllOf;
            //OneOf oneOf = element as OneOf;
            //OneOrMore oneOrMore = element as OneOrMore;
            //ZeroOrMore zeroOrMore = element as ZeroOrMore;
            //Optional optional = element as Optional;
            
            if (patternIdentifier != null)
            {
                tokenTypes.Add(patternIdentifier.Name.ToIdentifier());
            }
            else if (defIdentifier != null)
            {
                Def def = grammar.Defs.Single(x => x.Name == defIdentifier.Name);

                foreach (var element1 in def.Elements)
                {
                    GetTokenTypes(grammar, element1, ref tokenTypes);
                }
            }
            else
            {
                foreach (var identifier in element.Elements)
                {
                    GetTokenTypes(grammar, identifier, ref tokenTypes);
                }
            }
        }
Пример #4
0
        private List<Element> GetOthers(Element element)
        {
            List<Element> others = new List<Element>();

            GetOthers(element, others);

            return others;
        }
Пример #5
0
        private void GetOthers(Element element, List<Element> others)
        {
            Element parent = element.Parent;

            while (parent != null)
            {
                if (parent is Optional || parent is Def)
                {
                    foreach (var child in parent.Elements.Where(x => x != element))
                    {
                        others.Add(child);
                    }
                }
                parent = parent.Parent;
            }
        }
Пример #6
0
 private void SetParents(Element element)
 {
     foreach (var child in element.Elements)
     {
         child.Parent = element;
         SetParents(child);
     }
 }
Пример #7
0
        private string GetAreTokenTypes(Grammar grammar, Element element)
        {
            List<string> tokenTypes = GetTokenTypes(grammar, element);
            List<List<string>> otherTokenTypes = new List<List<string>>();

            var others = GetOthers(element);

            foreach (var element2 in others)
            {
                otherTokenTypes.Add(GetTokenTypes(grammar, element2));
            }

            string[] names = { };

            if (otherTokenTypes.Count == 0)
            {
                names = new[] { tokenTypes[0] };
            }
            else
            {
                for (int i = 0; i < tokenTypes.Count; ++i)
                {
                    names = tokenTypes.Take(i + 1).ToArray();

                    var tokenTypesString = String.Join(".", names);

                    if (otherTokenTypes.All(x => String.Join(".", x.Take(i + 1)) != tokenTypesString))
                    {
                        break;
                    }
                }
            }
            return $"AreTokenTypes({ String.Join(", ", names.Select(x => $"TokenType.{x.ToIdentifier()}")) })";
        }
Пример #8
0
 protected ElementWithSingleChild(Element element)
 {
     Element = element;
 }
Пример #9
0
 public ZeroOrMore(Element element)
     : base(element)
 {
 }
Пример #10
0
 public Optional(Element element)
     : base(element)
 {
 }
Пример #11
0
 public OneOrMore(Element element)
     : base(element)
 {
 }
Пример #12
0
        private void ElementToString(StringBuilder stringBuilder, Element element)
        {
            Element patternIdentifier = element as PatternIdentifier;
            Element defIdentifier = element as DefIdentifier;
            OneOf oneOf = element as OneOf;
            AllOf allOf = element as AllOf;
            Optional optional = element as Optional;
            OneOrMore oneOrMore = element as OneOrMore;
            ZeroOrMore zeroOrMore = element as ZeroOrMore;

            if (patternIdentifier != null)
            {
                stringBuilder.Append(MakeSafe(patternIdentifier.Name));
            }
            else if (defIdentifier != null)
            {
                stringBuilder.Append(MakeSafe(defIdentifier.Name));
            }
            else if (oneOf != null)
            {
                stringBuilder.Append(String.Join(" | ", oneOf.Elements.Select(x => MakeSafe(x.Name))));
            }
            else if (allOf != null)
            {
                stringBuilder.Append(String.Join(" ", allOf.Elements.Select(x => MakeSafe(x.Name))));
            }
            else if (optional != null)
            {
                stringBuilder.Append("[");
                ElementToString(stringBuilder, optional.Element);
                stringBuilder.Append("]");
            }
            else if (oneOrMore != null)
            {
                ElementToString(stringBuilder, oneOrMore.Element);
                stringBuilder.Append("+");
            }
            else if (zeroOrMore != null)
            {
                ElementToString(stringBuilder, zeroOrMore.Element);
                stringBuilder.Append("*");
            }
        }