Пример #1
0
 /// <summary>
 /// Constructs a new instance of the parser
 /// </summary>
 /// <param name="parseTable">The parse table to use</param>
 /// <param name="tokenizer">The tokenizer to use </param>
 /// <param name="startSymbol">The start symbol</param>
 public DebugLL1Parser(Cfg cfg,
                       IEnumerable <Token> tokenizer)
 {
     _cfg = cfg;
     _PopulateAttrs();
     _parseTable        = cfg.ToLL1ParseTable();
     _stack             = new Stack <string>();
     _errorToken.Symbol = null;
     Restart(tokenizer);
 }
Пример #2
0
        static CodeTypeDeclaration _CreateParserClass(Cfg cfg, string name)
        {
            var sm   = new Dictionary <string, int>();
            var ii   = 0;
            var syms = new List <string>();

            cfg.FillSymbols(syms);


            foreach (var sym in syms)
            {
                sm.Add(sym, ii);
                ++ii;
            }
            var pt        = cfg.ToLL1ParseTable();
            var ipt       = pt.ToLL1Array(syms);
            var nodeFlags = new int[syms.Count];

            for (var i = 0; i < nodeFlags.Length; ++i)
            {
                var o = cfg.AttributeSets.GetAttribute(syms[i], "hidden", false);
                if (o is bool && (bool)o)
                {
                    nodeFlags[i] |= 2;
                }
                o = cfg.AttributeSets.GetAttribute(syms[i], "collapsed", false);
                if (o is bool && (bool)o)
                {
                    nodeFlags[i] |= 1;
                }
            }
            var attrSets = new KeyValuePair <string, object> [syms.Count][];

            for (ii = 0; ii < attrSets.Length; ii++)
            {
                AttributeSet attrs;
                if (cfg.AttributeSets.TryGetValue(syms[ii], out attrs))
                {
                    attrSets[ii] = new KeyValuePair <string, object> [attrs.Count];
                    var j = 0;
                    foreach (var attr in attrs)
                    {
                        attrSets[ii][j] = new KeyValuePair <string, object>(attr.Key, attr.Value);
                        ++j;
                    }
                }
                else
                {
                    attrSets[ii] = null;                    // new KeyValuePair<string, object>[0];
                }
            }


            var result = new CodeTypeDeclaration();

            result.Name       = name;
            result.Attributes = MemberAttributes.FamilyOrAssembly;
            result.BaseTypes.Add(typeof(LL1TableParser));
            CodeMemberField f;

            foreach (var s in syms)
            {
                if (null != s)
                {
                    f                = new CodeMemberField();
                    f.Attributes     = MemberAttributes.Const | MemberAttributes.Public;
                    f.Name           = s.Replace("#", "_").Replace("'", "_").Replace("<", "_").Replace(">", "_");
                    f.Type           = new CodeTypeReference(typeof(int));
                    f.InitExpression = CodeDomUtility.Serialize(cfg.GetIdOfSymbol(s));
                    result.Members.Add(f);
                }
            }
            f                = new CodeMemberField();
            f.Attributes     = MemberAttributes.Static;
            f.Name           = "_Symbols";
            f.Type           = new CodeTypeReference(typeof(string[]));
            f.InitExpression = CodeDomUtility.Serialize(syms.ToArray());
            result.Members.Add(f);

            f                = new CodeMemberField();
            f.Attributes     = MemberAttributes.Static;
            f.Name           = "_ParseTable";
            f.Type           = new CodeTypeReference(typeof(int[][][]));
            f.InitExpression = CodeDomUtility.Serialize(ipt);
            result.Members.Add(f);

            f                = new CodeMemberField();
            f.Attributes     = MemberAttributes.Static;
            f.Name           = "_InitCfg";
            f.Type           = new CodeTypeReference(typeof(int[]));
            f.InitExpression = CodeDomUtility.Serialize(new int[] { cfg.GetIdOfSymbol(cfg.StartSymbol), cfg.FillNonTerminals().Count });
            result.Members.Add(f);

            f                = new CodeMemberField();
            f.Attributes     = MemberAttributes.Static;
            f.Name           = "_NodeFlags";
            f.Type           = new CodeTypeReference(typeof(int[]));
            f.InitExpression = CodeDomUtility.Serialize(nodeFlags);
            result.Members.Add(f);

            f                = new CodeMemberField();
            f.Attributes     = MemberAttributes.Static;
            f.Name           = "_AttributeSets";
            f.Type           = new CodeTypeReference(attrSets.GetType());
            f.InitExpression = CodeDomUtility.Serialize(attrSets);
            result.Members.Add(f);

            var ctor = new CodeConstructor();

            ctor.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IEnumerable <Token>), "tokenizer"));
            ctor.BaseConstructorArgs.AddRange(new CodeExpression[] {
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(result.Name), "_ParseTable"),
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(result.Name), "_InitCfg"),
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(result.Name), "_Symbols"),
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(result.Name), "_NodeFlags"),
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(result.Name), "_AttributeSets"),
                new CodeArgumentReferenceExpression("tokenizer")
            });
            ctor.Attributes = MemberAttributes.Public;
            result.Members.Add(ctor);
            return(result);
        }