Esempio n. 1
0
        internal Terminal LookupOrDefineTerminal(Token token, string name, string alias, LexSpan span)
        {
            bool     isIdent = (token == Token.ident);
            Terminal result  = null;

            // Canonicalize escaped char-literals
            if (!isIdent)
            {
                name = CharacterUtilities.CanonicalizeCharacterLiteral(name, 1);
            }
            else if (alias != null)
            {
                alias = CharacterUtilities.CanonicalizeAlias(alias);
            }
            // Check if already present in dictionary
            if (!terminals.ContainsKey(name))      // terminal not already known
            {
                result          = new Terminal(isIdent, name, alias);
                terminals[name] = result;
                if (alias != null)
                {
                    CheckAndSetAlias(alias, result, span);
                }
            }
            else
            {
                result = terminals[name];
                if (alias != null)
                {
                    CheckAlias(alias, result, span);
                }
            }
            return(result);
        }
Esempio n. 2
0
        internal static bool BumpsMax(string str)
        {
            string num = CharacterUtilities.CanonicalizeCharacterLiteral(str, 1);
            int    ord = CharacterUtilities.OrdinalOfCharacterLiteral(str, 1);

            return(ord > Terminal.max);
        }
Esempio n. 3
0
        internal static void InsertMaxDummyTerminalInDictionary(Dictionary <string, Terminal> table)
        {
            Terminal maxTerm = null;

            if (Terminal.Max != 0)
            {
                string maxChr = CharacterUtilities.QuoteMap(Terminal.Max);   // FIXME
                maxTerm = table[maxChr];
            }
            table["@Max@"] = maxTerm;
        }
Esempio n. 4
0
 public override string ToString()
 {
     if (this.alias != null)
     {
         return(CharacterUtilities.QuoteAndCanonicalize(this.alias));
     }
     else
     {
         return(base.ToString());
     }
 }
Esempio n. 5
0
        internal Terminal LookupTerminal(Token token, string name)
        {
            bool isIdent = (token == Token.ident);

            // Canonicalize escaped char-literals
            if (!isIdent)
            {
                name = CharacterUtilities.CanonicalizeCharacterLiteral(name, 1);
            }
            // Check if already present in dictionary
            if (!terminals.ContainsKey(name))  // else insert ...
            {
                terminals[name] = new Terminal(isIdent, name);
            }
            return(terminals[name]);
        }
Esempio n. 6
0
 /// <summary>
 /// If name is an escaped char-lit, it must already be
 /// canonicalized according to some convention. In this
 /// application CharUtils.Canonicalize().
 /// </summary>
 /// <param name="symbolic">Means "is an ident, not a literal character"</param>
 /// <param name="name">string representation of symbol</param>
 internal Terminal(bool symbolic, string name)
     : base(name)
 {
     this.symbolic = symbolic;
     if (symbolic)
     {
         this.n = ++count;
     }
     else
     {
         this.n = CharacterUtilities.OrdinalOfCharacterLiteral(name, 1);
         if (n > max)
         {
             max = n;
         }
     }
 }
Esempio n. 7
0
        /// <summary>
        /// This static method expands characters in a literal
        /// string, returning a modified string with character
        /// escapes replaced by the character which they denote.
        /// This includes characters outside the BMP which
        /// return a pair of surrogate characters.
        /// </summary>
        /// <param name="source">the input string</param>
        /// <returns>interpreted version of the string</returns>
        public static string InterpretCharacterEscapes(string source)
        {
            int sLen = source.Length;

            if (sLen == 0)
            {
                return(source);
            }
            char[] arr  = new char[sLen];
            int    sNxt = 0;
            int    aNxt = 0;
            char   chr  = source[sNxt++];

            for (; ; chr = source[sNxt++])
            {
                if (chr != '\\')
                {
                    arr[aNxt++] = chr;
                }
                else
                {
                    int codePoint = EscapedChar(source, ref sNxt);
                    if (codePoint > 0xFFFF)
                    {
                        arr[aNxt++] = CharacterUtilities.HiSurrogate(codePoint);
                        arr[aNxt++] = CharacterUtilities.LoSurrogate(codePoint);
                    }
                    else
                    {
                        arr[aNxt++] = (char)codePoint;
                    }
                }
                if (sNxt == sLen)
                {
                    return(new String(arr, 0, aNxt));
                }
            }
        }
        private void GenerateInitializeMethod(
            List <AutomatonState> states,
            List <Production> productions,
            Dictionary <string, NonTerminal> nonTerminals)
        {
            // warning 649 : this field never assigned to.
            Console.WriteLine("#pragma warning disable 649");
            Console.WriteLine("  private static Dictionary<int, string> aliases;");
            Console.WriteLine("#pragma warning restore 649");
            Console.WriteLine("  private static Rule[] rules = new Rule[{0}];", productions.Count + 1);
            Console.WriteLine("  private static State[] states = new State[{0}];", states.Count);
            Console.WriteLine("  private static string[] nonTerms = new string[] {");

            int length = 0;

            Console.Write("      ");
            foreach (NonTerminal nonTerminal in nonTerminals.Values)
            {
                string ss = String.Format(CultureInfo.InvariantCulture, "\"{0}\", ", nonTerminal.ToString());
                length += ss.Length;
                Console.Write(ss);
                if (length > 70)
                {
                    Console.WriteLine();
                    Console.Write("      ");
                    length = 0;
                }
            }
            Console.WriteLine("};");
            Console.WriteLine();

            Console.WriteLine("  static {0}() {{", grammar.ParserName);
            int state_nr = 0;

            foreach (AutomatonState state in states)
            {
                GenerateShiftReduceMachineState(state_nr++, state);
            }
            Console.WriteLine();

            Console.WriteLine("    for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo;");

            Console.WriteLine();
            foreach (Production production in productions)
            {
                GenerateShiftReduceMachineRule(production);
            }

            List <Terminal> aliasList = new List <Terminal>();

            foreach (KeyValuePair <string, Terminal> pair in grammar.terminals)
            {
                Terminal term = pair.Value;
                if (term.Alias != null)
                {
                    aliasList.Add(term);
                }
            }
            if (aliasList.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("    aliases = new Dictionary<int, string>();");
                foreach (Terminal termWithAlias in aliasList)
                {
                    Console.WriteLine("    aliases.Add({0}, {1});",
                                      termWithAlias.num,
                                      CharacterUtilities.QuoteAndCanonicalize(termWithAlias.Alias));
                }
            }
            Console.WriteLine("  }");
            Console.WriteLine();

            Console.WriteLine("  protected override void Initialize() {");
            Console.WriteLine("    this.InitSpecialTokens((int){0}.error, (int){0}.EOF);", grammar.TokenName);
            Console.WriteLine("    this.InitStates(states);");
            Console.WriteLine("    this.InitRules(rules);");
            Console.WriteLine("    this.InitNonTerminals(nonTerms);");
            Console.WriteLine("  }");
            Console.WriteLine();
        }