Esempio n. 1
0
        static void Main(string[] args)
        {
            string[] IgnoreClassifiers = new string[] { "Whitespace"};
            //var parser = DynamicParser.LoadFromMgx("M.mgx", "Microsoft.M.MParser");
            var parser = DynamicParser.LoadFromMgx("Mg.mgx", "Microsoft.M.Grammar.MGrammar");
            using (var sr = new StreamReader("rockfordauth.mg"))
            using (var parseContext = new ParseContext(parser.Lexer,
                                                       parser.Parser,
                                                       parser.GraphBuilder,
                                                       ErrorReporter.Standard,
                                                       "test.M"))
            {
                var lexerReader = new LexerReader();
                if (lexerReader.Open(parseContext, sr, true))
                {
                    using (StreamWriter sw = new StreamWriter("output.html"))
                    {
                       sw.WriteLine("<html><head><style>body { background-color: #333333; color: white; font-family: Consolas; } .Delimiter { color: #ddd; } .Keyword { color: #6dcff6; font-weight: bold; } .Literal { color: #10cc20; }</style></head><body>");
                        bool eof = false;
                        while (true)
                        {
                            var tokens = lexerReader.Read();
                            foreach (var token in tokens)
                            {
                                object[] tokenInfos = parser.GetTokenInfo(token.Tag);
                                ClassificationAttribute classificationAttribute = null;
                                if (tokenInfos != null && tokenInfos.Length > 0)
                                {
                                    classificationAttribute = tokenInfos[0] as ClassificationAttribute;
                                }

                                if (token.Description.Equals("EOF")) // TODO: Match against the EOF token if its public
                                {
                                    eof = true;
                                    break;
                                }
                                if (classificationAttribute != null &&
                                    !IgnoreClassifiers.Contains(classificationAttribute.Classification))
                                {
                                    sw.Write(string.Format("<span class=\"{0}\">{1}</span>",
                                        classificationAttribute.Classification,
                                        token.GetTextString()));
                                }
                                else
                                {
                                    string output = token.GetTextString().Replace(" ", "&nbsp;").Replace("\r", "<br />");
                                    sw.Write(output);
                                }
                            }
                            if (eof)
                                break;
                        }
                        sw.WriteLine("</body></html>");
                    }
                }
                Console.WriteLine("Output generated.");
                Console.ReadLine();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Reads a numeric literal token.
        /// </summary>
        /// <param name="firstChar"> The first character of the token. </param>
        /// <returns> A numeric literal token. </returns>
        private Token ReadNumericLiteral(int firstChar)
        {
            // We need to keep track of the column and possibly capture the input into a string.
            var reader = new LexerReader(this);

            NumberParser.ParseCoreStatus status;
            double result = NumberParser.ParseCore(reader, (char)firstChar, out status);

            // Handle various error cases.
            switch (status)
            {
            case NumberParser.ParseCoreStatus.NoDigits:
                // If the number consists solely of a period, return that as a token.
                return(PunctuatorToken.Dot);

            case NumberParser.ParseCoreStatus.NoExponent:
                throw new SyntaxErrorException("Invalid number.", this.lineNumber, this.Source.Path);

            case NumberParser.ParseCoreStatus.InvalidHexLiteral:
                throw new SyntaxErrorException("Invalid hexidecimal literal.", this.lineNumber, this.Source.Path);

            case NumberParser.ParseCoreStatus.ES3OctalLiteral:
                // ES3 octal literals are not supported in strict mode.
                if (this.StrictMode)
                {
                    throw new SyntaxErrorException("Octal numbers are not allowed in strict mode.", this.lineNumber, this.Source.Path);
                }
                break;

            case NumberParser.ParseCoreStatus.InvalidOctalLiteral:
                throw new SyntaxErrorException("Invalid octal literal.", this.lineNumber, this.Source.Path);

            case NumberParser.ParseCoreStatus.InvalidBinaryLiteral:
                throw new SyntaxErrorException("Invalid binary literal.", this.lineNumber, this.Source.Path);
            }

            // Return the result as an integer if possible, otherwise return it as a double.
            if (result == (double)(int)result)
            {
                return(new LiteralToken((int)result));
            }
            return(new LiteralToken(result));
        }
Esempio n. 3
0
        /// <summary>
        /// Reads a numeric literal token.
        /// </summary>
        /// <param name="firstChar"> The first character of the token. </param>
        /// <returns> A numeric literal token. </returns>
        private Token ReadNumericLiteral(int firstChar)
        {
            // We need to keep track of the column and possibly capture the input into a string.
            var reader = new LexerReader(this);

            NumberParser.ParseCoreStatus status;
            double result = NumberParser.ParseCore(reader, (char)firstChar, out status);

            // Handle various error cases.
            switch (status)
            {
            case NumberParser.ParseCoreStatus.NoDigits:
                // If the number consists solely of a period, return that as a token.
                return(PunctuatorToken.Dot);

            case NumberParser.ParseCoreStatus.NoExponent:
                throw new JavaScriptException(this.engine, "SyntaxError", "Invalid number.", this.lineNumber, this.Source.Path);

            case NumberParser.ParseCoreStatus.InvalidHexLiteral:
                throw new JavaScriptException(this.engine, "SyntaxError", "Invalid hexidecimal constant.", this.lineNumber, this.Source.Path);

            case NumberParser.ParseCoreStatus.OctalLiteral:
                // Octal number are only supported in ECMAScript 3 compatibility mode.
                if (this.engine.CompatibilityMode != CompatibilityMode.ECMAScript3)
                {
                    throw new JavaScriptException(this.engine, "SyntaxError", "Octal numbers are not supported.", this.lineNumber, this.Source.Path);
                }
                break;

            case NumberParser.ParseCoreStatus.InvalidOctalLiteral:
                throw new JavaScriptException(this.engine, "SyntaxError", "Invalid octal constant.", this.lineNumber, this.Source.Path);
            }

            // Return the result as an integer if possible, otherwise return it as a double.
            if (result == (double)(int)result)
            {
                return(new LiteralToken((int)result));
            }
            return(new LiteralToken(result));
        }
Esempio n. 4
0
        /// <summary>
        /// Reads a numeric literal token.
        /// </summary>
        /// <param name="firstChar"> The first character of the token. </param>
        /// <returns> A numeric literal token. </returns>
        private Token ReadNumericLiteral(int firstChar)
        {
            // We need to keep track of the column and possibly capture the input into a string.
            var reader = new LexerReader(this);

            NumberParser.ParseCoreStatus status;
            double result = NumberParser.ParseCore(reader, (char)firstChar, out status);

            // Handle various error cases.
            switch (status)
            {
                case NumberParser.ParseCoreStatus.NoDigits:
                    // If the number consists solely of a period, return that as a token.
                    return PunctuatorToken.Dot;
                case NumberParser.ParseCoreStatus.NoExponent:
                    throw new JavaScriptException(this.engine, "SyntaxError", "Invalid number.", this.lineNumber, this.Source.Path);
                case NumberParser.ParseCoreStatus.InvalidHexLiteral:
                    throw new JavaScriptException(this.engine, "SyntaxError", "Invalid hexidecimal constant.", this.lineNumber, this.Source.Path);
                case NumberParser.ParseCoreStatus.OctalLiteral:
                    // Octal number are only supported in ECMAScript 3 compatibility mode.
                    if (this.engine.CompatibilityMode != CompatibilityMode.ECMAScript3)
                        throw new JavaScriptException(this.engine, "SyntaxError", "Octal numbers are not supported.", this.lineNumber, this.Source.Path);
                    break;
                case NumberParser.ParseCoreStatus.InvalidOctalLiteral:
                    throw new JavaScriptException(this.engine, "SyntaxError", "Invalid octal constant.", this.lineNumber, this.Source.Path);
            }

            // Return the result as an integer if possible, otherwise return it as a double.
            if (result == (double)(int)result)
                return new LiteralToken((int)result);
            return new LiteralToken(result);
        }
Esempio n. 5
0
 public Lexer()
 {
     reader        = new LexerReader();
     ruleFirstKeys = string.Empty;
     ruleEndKeys   = string.Empty;
 }
Esempio n. 6
0
 public Lexer(LexerReader reader)
 {
     this.reader = reader;
 }
Esempio n. 7
0
 public Lexer(IText src)
 {
     reader = new LexerReader(src);
 }
Esempio n. 8
0
 public Lexer()
 {
     reader = new LexerReader();
     ruleFirstKeys = string.Empty;
     ruleEndKeys = string.Empty;
 }
Esempio n. 9
0
 public Lexer(LexerReader reader)
 {
     this.reader = reader;
 }
Esempio n. 10
0
 public Lexer(IText src)
 {
     reader = new LexerReader(src);
 }