Exemplo n.º 1
0
        public GenEntitasLangParser(G.Contexts contexts)
        {
            _contexts = contexts;
            for (var i = 0; i < _contexts.main.contextInfo.componentTypes.Length; i++)
            {
                _mainTypesToI[_contexts.main.contextInfo.componentTypes[i]] = i;
            }

            Comments = new CommentParser(  );

            Identifier =
                from first in Parse.Letter.Once()
                from rest in Parse.LetterOrDigit.XOr(Parse.Char('_')).Many()
                select new string(first.Concat(rest).ToArray());

            QuotedString =
                from openQuote in Parse.Char('"')
                from content in Parse.AnyChar.Except(Parse.Char('"')).Many(  )
                from closeQuote in Parse.Char('"')
                select new String(content.ToArray(  ));

            QuotedIdentifier =
                from openQuote in Parse.Char('"')
                from content in Identifier
                from closeQuote in Parse.Char('"')
                select new String(content.ToArray(  ));

            AliasGet =
                (from aliasKey in Identifier
                 select aliasKey)
                .Select(key =>
            {
                if (!_contexts.main.hasAliasComp || !_contexts.main.aliasComp.Values.ContainsKey(key))
                {
                    throw new KeyNotFoundException($"Alias '{key}' not found");
                }
                return(_contexts.main.aliasComp.Values[key]);
            });

            CompContextNames =
                from contextsKeyword in Parse.String("in")
                from ws in Parse.WhiteSpace.AtLeastOnce(  )
                from contextNames in Identifier.DelimitedBy(Parse.Char(',').Token(  )).Token(  )
                select new G.ContextNamesComp
            {
                Values = contextNames.ToList(  )
            };

            FieldInfo =
                from fieldName in Identifier
                from colon in Parse.Char(':').Token(  )
                from typeName in AliasGet.Or(QuotedString)
                select new G.FieldInfo(typeName, fieldName);

            CompPublicFields =
                from ws in Parse.WhiteSpace.Many(  )
                from publicFieldsKeyword in Parse.String("publicFields")
                from colon in Parse.Char(':').Token(  )
                from fieldInfos in FieldInfo.Token(  ).AtLeastOnce(  )
                select new G.PublicFieldsComp
            {
                Values = fieldInfos.ToList(  )
            };

            CompUnique =
                from uniqueKeyword in Parse.String("unique").Token(  )
                select new G.UniqueComp(  );

            CompParam =
                CompContextNames
                .Or(CompPublicFields)
                .Or(CompUnique)
            ;

            CompEnt =
                from ws11 in Parse.WhiteSpace.Many(  )
                from compKeyword in Parse.String("comp")
                from ws in Parse.WhiteSpace.AtLeastOnce(  )
                from id in Identifier.Token(  )
                from comps in CompParam.XMany(  )
                select AddComp(_contexts, id, comps);

            AliasRule =
                from key in Identifier
                from colon in Parse.Char(':').Token(  )
                from value in QuotedString
                select new KeyValuePair <String, String>(key, value);

            Alias =
                (from ws11 in Parse.WhiteSpace.Many(  )
                 from aliasKeyword in Parse.String("alias")
                 from ws in Parse.WhiteSpace.AtLeastOnce(  )
                 from aliasKvs in AliasRule.Token(  ).AtLeastOnce(  )
                 select aliasKvs)
                .Select(keyValues =>
            {
                foreach (var kv in keyValues)
                {
                    AddAlias(_contexts, kv.Key, kv.Value);
                }
                return(_contexts.main.aliasCompEntity);
            });

            AliasBlock =
                from aliasList in Alias.Many(  )
                select _contexts.main.aliasCompEntity;

            CompBlock =
                from comps in CompEnt.AtLeastOnce(  )
                select comps;

            var anyCharTillComment =
                Parse.AnyChar.Until(Comments.AnyComment).Text(  );

            var anyCharTillLineEnd =
                from s in Parse.AnyChar.Until(Parse.LineEnd).Text(  )
                select s + '\n';

            RemoveCommentsParser =
                (from nonComment in
                 anyCharTillComment
                 .Or(anyCharTillLineEnd)
                 .Or(Parse.AnyChar.Many(  ).Text(  ))
                 select nonComment).Many(  )
                .Select(values => String.Concat(values));

            Root =
                from aliases in AliasBlock.Optional(  )
                from comps in CompBlock
                select _contexts;
        }