Example #1
0
        public object GetAttribute(string symbol, string name, object @default = null)
        {
            LexAttributeList l;

            if (AttributeSets.TryGetValue(symbol, out l))
            {
                var i = l.IndexOf(name);
                if (-1 < i)
                {
                    return(l[i].Value);
                }
            }
            return(@default);
        }
        public void WriteCSharpSymbolConstantsTo(TextWriter writer, string modifiers)
        {
            if (null == modifiers)
            {
                modifiers = "";
            }
            var names = new HashSet <string>();

            writer.WriteLine(string.Concat("\t", modifiers, " const int EOS=", GetSymbolId("#EOS").ToString(), ";"));
            names.Add("EOS");
            writer.WriteLine(string.Concat("\t", modifiers, " const int ERROR=", GetSymbolId("#ERROR").ToString(), ";"));
            names.Add("ERROR");
            foreach (var sym in _EnumSymbols())
            {
                IDictionary <string, object> d;
                object o;
                if (AttributeSets.TryGetValue(sym, out d) && d.TryGetValue("hidden", out o) && o is bool && (bool)o)
                {
                    continue;
                }
                if (AttributeSets.TryGetValue(sym, out d) && d.TryGetValue("collapse", out o) && o is bool && (bool)o)
                {
                    continue;
                }
                var sid = Convert.ToString(sym);
                if (_IsValidIdentifier(sid))
                {
                    string s;
                    if (!string.IsNullOrEmpty(modifiers))
                    {
                        s = string.Concat("\t", modifiers, " const int ");
                    }
                    else
                    {
                        s = string.Concat("\t", modifiers, "const int ");
                    }

                    var id = GetSymbolId(sym);
                    s = string.Concat(s, CS.CreateEscapedIdentifier(sid.Replace('-', '_')), " = ");
                    s = _GetUniqueName(names, s);
                    names.Add(s);
                    s = string.Concat(s, id.ToString(), ";");
                    writer.WriteLine(s);
                }
            }
        }
Example #3
0
        public Parser ToLL1Parser(IEnumerable <Token> tokenizer = null)
        {
            var parseTable = ToLL1ParseTable();
            var syms       = new List <string>();

            FillSymbols(syms);
            var nodeFlags = new int[syms.Count];

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

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

            return(new LL1TableParser(parseTable.ToLL1Array(syms), initCfg, syms.ToArray(), nodeFlags, attrSets, tokenizer));
        }