Beispiel #1
0
 // This constructor ignores explicit numeric value declarations
 // This might change later ...
 internal TokenInfo(LexSpan name, LexSpan alias)
 {
     this.name = name.ToString();
     if (alias != null)
     {
         this.alias = alias.ToString();
     }
 }
 internal void AddWarning(int code, string msg, LexSpan spn)
 {
     if (spn == null)
     {
         spn = DefaultSpan;
     }
     this.AddError(new Error(code, msg, spn, true)); wrnNum++;
 }
 internal void AddError(int code, string msg, LexSpan spn)
 {
     if (spn == null)
     {
         spn = DefaultSpan;
     }
     this.AddError(new Error(code, msg, spn, false)); errNum++;
 }
Beispiel #4
0
        internal static string GetVerbatimString(LexSpan span)
        {
            string text = span.ToString();

            if (text[0] != '@' || text[1] != '\"' || text[text.Length - 1] != '\"')
            {
                throw new GppgInternalException("Internal error: invalid litstring");
            }
            text = text.Substring(2, text.Length - 3);
            return(CharacterUtilities.InterpretEscapesInVerbatimString(text));
        }
Beispiel #5
0
        // ===============================================================
        //
        //  Various helpers for the semantic actions of the parser
        //  Definition Part Helpers
        //
        // ===============================================================

        internal void SetSemanticType(LexSpan span)
        {
            if (grammar.ValueTypeNameSpan != null)
            {
                handler.ListError(grammar.ValueTypeNameSpan, 72);
                handler.ListError(span, 72);
            }
            else
            {
                grammar.ValueTypeNameSpan = span;
                grammar.ValueTypeName     = span.ToString();
            }
        }
Beispiel #6
0
        internal string GetLitString(LexSpan span)
        {
            string text = span.ToString();

            if (text[0] != '\"' || text[text.Length - 1] != '\"')
            {
                throw new GppgInternalException("Internal error: invalid litstring");
            }
            text = text.Substring(1, text.Length - 2);
            try
            {
                text = CharacterUtilities.InterpretCharacterEscapes(text);
            }
            catch (StringInterpretException e)
            {
                handler.ListError(span, 70, e.Message, '\'');
            }
            return(text);
        }
Beispiel #7
0
        // ===============================================================
        //
        //  Various helpers for the semantic actions of the parser
        //  Rules Part Helpers
        //
        // ===============================================================

        private void SetCurrentLHS(LexSpan lhs)
        {
            string      lhsName = lhs.ToString();
            NonTerminal nt      = grammar.LookupNonTerminal(lhsName);

            if (grammar.terminals.ContainsKey(lhsName))
            {
                handler.ListError(lhs, 76);
            }
            currentLHS = nt;
            if (grammar.startSymbol == null)
            {
                grammar.startSymbol = nt;
            }

            if (grammar.productions.Count == 0)
            {
                grammar.CreateSpecialProduction(grammar.startSymbol);
            }
        }
Beispiel #8
0
 private void DeclareTokens(LexSpan span1, PrecType prop, string kind, List <TokenInfo> list)
 {
     grammar.BumpPrec();
     if (GPCG.ImportedTokens)
     {
         handler.ListError(span1, 81);
     }
     foreach (TokenInfo info in list)
     {
         Token    token = (IsLitChar(info.name) ? Token.litchar : Token.ident);
         Terminal t     = grammar.LookupOrDefineTerminal(token, info.name, info.alias, span1);
         if (prop != PrecType.token)
         {
             t.prec = new Precedence(prop, grammar.Prec);
         }
         if (!String.IsNullOrEmpty(kind))
         {
             t.kind = kind;
         }
     }
 }
Beispiel #9
0
 internal ActionProxy(LexSpan precedence, LexSpan identifier, LexSpan code)
 {
     codeBlock       = code;
     precedenceToken = identifier;
     precedenceSpan  = precedence;
 }
Beispiel #10
0
        /// <summary>
        /// This method is called when the divider "%%" signals the
        /// end of the defnitions section and the start of productions.
        /// </summary>
        /// <param name="def">The divider token text span</param>
        private void TidyUpDefinitions(LexSpan def)
        {
            handler.DefaultSpan = def;
            if (GPCG.Defines)
            {
                grammar.TokFileName = baseName + ".tokens";
            }
            if (GPCG.CsTokenFile)
            {
                grammar.CsTokFileName = baseName + "Tokens.cs";
            }
            if (GPCG.ShareTokens)
            {
                grammar.DatFileName = baseName + "Tokens.dat";
            }
            if (GPCG.Conflicts)
            {
                grammar.DiagFileName = baseName + ".conflicts";
            }
            // If both %union AND %YYSTYPE have been set, YYSTYPE must be
            // a simple name, and not a type-constructor. Check this now!
            if (grammar.unionType != null &&
                grammar.ValueTypeName != null &&
                grammar.ValueTypeName.LastIndexOfAny(new char[] { '.', '[', '<' }) > 0)
            {
                handler.ListError(grammar.ValueTypeNameSpan, 71);
            }
            // If %importtokens has been declared then there must be no
            // other %token, %left, %right and so on declarations.
            if (GPCG.ImportedTokens)
            {
                // Terminal should only contain the two token
                // values added by default: error, and EOF.
                if (grammar.terminals.Count > 2)
                {
                    handler.ListError(def, 79);
                }
                if (GPCG.ShareTokens)
                {
                    handler.ListError(def, 80);
                }
                if (GPCG.CsTokenFile)
                {
                    handler.ListError(def, 82);
                }

                FileStream fStrm = null;
                try {
                    fStrm = new FileStream(grammar.DatFileName, FileMode.Open);
                    BinaryFormatter formatter = new BinaryFormatter();
                    grammar.terminals = (Dictionary <string, Terminal>)formatter.Deserialize(fStrm);
                    Terminal.RemoveMaxDummyTerminalFromDictionary(grammar.terminals);
                }
                catch (Exception x) {
                    Console.Error.WriteLine("GPPG: Error. Failed to deserialize file {0}", grammar.DatFileName);
                    Console.Error.WriteLine(x.Message);
                }
                finally {
                    if (fStrm != null)
                    {
                        fStrm.Close();
                    }
                }
            }
        }
        internal void ListError(LexSpan spn, int num)
        {
            string message;

            switch (num)
            {
            // Lexical Errors Detected by the Scanner ...
            case 50: message = "Unknown %keyword in this context"; break;

            case 51: message = "Bad format for decimal number"; break;

            case 52: message = "Bad format for hexadecimal number"; break;

            case 53: message = "Unterminated comment starts here"; break;

            case 54: message = "Only whitespace is permitted here"; break;

            case 55: message = "Code block has unbalanced braces '{','}'"; break;

            case 56: message = "Keyword \"%}\" is out of place here"; break;

            case 57: message = "This character is invalid in this context"; break;

            case 58: message = "Literal string terminated by EOL"; break;

            case 59: message = "Keyword must start in column-0"; break;

            case 60: message = "Premature termination of Code Block"; break;

            // Syntactic Errors Detected by the Parser ...
            case 71: message = "With %union, %YYSTYPE can only be a simple name"; break;

            case 72: message = "Duplicate definition of Semantic Value Type name"; break;

            case 73: message = "Semantic action index is out of bounds"; break;

            case 74: message = "Unknown special marker in semantic action"; break;

            case 75: message = "Bad separator character in list"; break;

            case 76: message = "This name already defined as a terminal symbol"; break;

            case 77: message = "Position of unmatched brace"; break;

            case 78: message = "Literal string terminated by end of line"; break;

            case 79: message = "Cannot define tokens AND declare %importtokens"; break;

            case 80: message = "Cannot declare %sharetokens AND %importtokens"; break;

            case 81: message = "Cannot declare %importtokens AND extra tokens"; break;

            case 82: message = "Cannot declare %importtokens AND csTokenFile"; break;

            // Warnings Issued by Either Scanner or Parser ...
            case 100: message = "Optional numeric code ignored in this version"; break;

            case 101: message = "%locations is the default in GPPG"; break;

            case 102: message = "Mid-rule %prec has no effect"; break;

            default: message = "Error " + Convert.ToString(num, CultureInfo.InvariantCulture); break;
            }
            this.AddError(new Error(num, message, spn, num >= Error.minWrn));
            if (num < Error.minWrn)
            {
                errNum++;
            }
            else
            {
                wrnNum++;
            }
        }
        /// <summary>
        /// Add this error to the error buffer.
        /// </summary>
        /// <param name="spn">The span to which the error is attached</param>
        /// <param name="num">The error number</param>
        /// <param name="key">The featured string</param>
        internal void ListError(LexSpan spn, int num, string key, char quote)
        {
            string s = (quote == '\0' ? "" : quote.ToString());

            ListError(spn, num, key, s, s);
        }
Beispiel #13
0
 public ScanObj(int t, ValueType val, LexSpan loc)
 {
     this.token = t; this.yylval = val; this.yylloc = loc;
 }