Exemplo n.º 1
0
        public static void Head(String filePath, Int32 bytes)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("File doesn't exists.");
            }

            using (FileStream handle = File.OpenRead(filePath))
            {
                var data = CInterop.ReadToEndAsASCII(handle);
                Console.WriteLine(String.Join(" ", data.Take(bytes).Select(ch => ch + $"({( ( Int32 ) ch ).ToString ( "X2" )})")));
            }
        }
Exemplo n.º 2
0
        private (String Raw, Double Value) ReadLuaHex( )
        {
            var rawNumber = new StringBuilder( );

            this.reader.Advance(2);
            rawNumber.Append("0x");

            String integer = this.reader.ReadStringWhile(LJUtils.IsXDigit), fractional = null, shift = null;

            rawNumber.Append(integer);

            if (this.reader.IsNext("."))
            {
                this.reader.Advance(1);
                rawNumber.Append('.');

                fractional = this.reader.ReadStringWhile(LJUtils.IsXDigit);
                rawNumber.Append(fractional);
            }

            if (this.reader.IsNext("p") || this.reader.IsNext("P"))
            {
                rawNumber.Append(this.reader.ReadString(1));

                if (this.reader.IsNext("+") || this.reader.IsNext("-"))
                {
                    rawNumber.Append(this.reader.ReadString(1));
                }

                shift = this.reader.ReadStringWhile(LJUtils.IsDigit);
                rawNumber.Append(shift);

                if (String.IsNullOrEmpty(shift))
                {
                    throw new LexException("Malformed number literal (missing exponent).", this.Location);
                }
            }

            if (String.IsNullOrEmpty(integer) && String.IsNullOrEmpty(fractional))
            {
                throw new LexException("Malformed number literal (missing both integral part and fractional part).", this.Location);
            }

            return(
                Raw : rawNumber.ToString( ),
                Value : CInterop.atof(rawNumber.ToString( ))
                );
        }
Exemplo n.º 3
0
        private (String Raw, Double Value) ReadLuaDec( )
        {
            var    rawNumber = new StringBuilder( );
            String integer = this.reader.ReadStringWhile(LJUtils.IsDigit), fractional = null, exp = null;

            rawNumber.Append(integer);

            if (this.Consume('.'))
            {
                rawNumber.Append('.');
                fractional = this.reader.ReadStringWhile(LJUtils.IsDigit);
                rawNumber.Append(fractional);
            }

            if (this.reader.IsNext("e") || this.reader.IsNext("E"))
            {
                rawNumber.Append(this.reader.ReadString(1));
                if (this.reader.IsNext("+") || this.reader.IsNext("-"))
                {
                    rawNumber.Append(this.reader.ReadString(1));
                }

                exp = this.reader.ReadStringWhile(LJUtils.IsDigit);
                rawNumber.Append(exp);

                if (String.IsNullOrEmpty(exp))
                {
                    throw new LexException("Malformed number literal.", this.Location);
                }
            }

            if (String.IsNullOrEmpty(integer) && String.IsNullOrEmpty(fractional))
            {
                throw new LexException("Malformed number literal.", this.Location);
            }

            return(
                Raw : rawNumber.ToString( ),
                Value : CInterop.atof(rawNumber.ToString( ))
                );
        }
Exemplo n.º 4
0
        /// <summary>
        /// </summary>
        /// <param name="input">Should be in Extended ASCII</param>
        /// <param name="isGlua"></param>
        public GLuaLexer(String input) : base(CInterop.ConvertStringToASCII(input))
        {
            input = CInterop.ConvertStringToASCII(input);
            if (input.Any(ch => ch > 255))
            {
                throw new LexException("Input data is not in Extended ASCII.", this.Location);
            }
            // Silently eat up BOM
            this.Consume("\xEF\xBB\xBF");

            this.consumeNewlinesAutomatically = true;
            this.whitespaceID = "whitespace";

            // This won't be really used tbh since the base lexer
            // class isn't ready to handle floating point numbers
            // just yet
            this.numberSettings = new IntegerLexSettings
            {
                DefaultType       = IntegerLexSettings.NumberType.Decimal,
                HexadecimalPrefix = "0x",
            };

            this.charSettings = new CharLexSettings
            {
                // \000 -> \255
                DecimalEscapePrefix   = "\\",
                DecimalEscapeMaxLengh = 3,
                DecimalEscapeMaxValue = 255,

                // \x00 -> \xFF
                HexadecimalEscapePrefix   = "\\x",
                HexadecimalEscapeMaxLengh = 2
            };

            // String settings. Not many pitfalls other than \
            // making a string multiline
            this.stringSettings = new StringLexSettings
            {
                CharSettings = this.charSettings,

                NewlineEscape = "\\"
            };

            this.stringSettings
            .CharSettings
            .RegisterEscapeConstant(@"\0", '\0')
            .RegisterEscapeConstant(@"\a", '\a')
            .RegisterEscapeConstant(@"\b", '\b')
            .RegisterEscapeConstant(@"\f", '\f')
            .RegisterEscapeConstant(@"\n", '\n')
            .RegisterEscapeConstant(@"\r", '\r')
            .RegisterEscapeConstant(@"\t", '\t')
            .RegisterEscapeConstant(@"\v", '\v')
            .RegisterEscapeConstant(@"\'", '\'')
            .RegisterEscapeConstant("\\\"", '"')
            .RegisterEscapeConstant(@"\\", '\\');

            var notalnum = new Func <Char, Boolean> (ch => !LJUtils.IsAlNum(ch));

            #region Binary Operators

            this.tokenManager
            .AddToken("=", "=", TokenType.Operator)
            // Arithmetic
            .AddToken("+", "+", TokenType.Operator)
            .AddToken("-", "-", TokenType.Operator, next => next != '-')
            .AddToken("%", "%", TokenType.Operator)
            .AddToken("/", "/", TokenType.Operator)
            .AddToken("*", "*", TokenType.Operator)
            .AddToken("^", "^", TokenType.Operator)
            // Binary
            .AddToken("<<", "<<", TokenType.Operator)
            .AddToken(">>", ">>", TokenType.Operator)
            .AddToken("|", "|", TokenType.Operator)
            .AddToken("&", "&", TokenType.Operator)
            .AddToken("~", "~", TokenType.Operator)
            // String
            .AddToken("..", "..", TokenType.Operator)
            // Boolean
            .AddToken("==", "==", TokenType.Operator)
            .AddToken("<", "<", TokenType.Operator)
            .AddToken("<=", "<=", TokenType.Operator)
            .AddToken("~=", "~=", TokenType.Operator)
            .AddToken("!=", "!=", TokenType.Operator)
            .AddToken(">", ">", TokenType.Operator)
            .AddToken(">=", ">=", TokenType.Operator)
            .AddToken("&&", "&&", TokenType.Operator)
            .AddToken("||", "||", TokenType.Operator)
            .AddToken("and", "and", TokenType.Operator, notalnum)
            .AddToken("or", "or", TokenType.Operator, notalnum);

            #endregion Binary Operators

            #region Unary Operators

            this.tokenManager
            .AddToken("not", "not", TokenType.Operator, notalnum)
            .AddToken("#", "#", TokenType.Operator)
            .AddToken("!", "!", TokenType.Operator);
            // - and ~ are in binary ops since they can be either.

            #endregion Unary Operators

            #region Symbols

            this.tokenManager
            .AddToken(",", ",", TokenType.Punctuation)
            .AddToken(".", ".", TokenType.Punctuation, ch => !LJUtils.IsDigit(ch))
            .AddToken(":", ":", TokenType.Punctuation)
            .AddToken("::", "::", TokenType.Punctuation)
            .AddToken(";", ";", TokenType.Punctuation)
            .AddToken("...", "...", TokenType.Identifier)
            .AddToken("(", "(", TokenType.LParen)
            .AddToken(")", ")", TokenType.RParen)
            .AddToken("[", "[", TokenType.LBracket, next => next != '[' && next != '=')
            .AddToken("]", "]", TokenType.RBracket)
            .AddToken("{", "{", TokenType.LCurly)
            .AddToken("}", "}", TokenType.RCurly);

            #endregion Symbols

            #region Keywords

            this.tokenManager
            .AddToken("break", "break", TokenType.Keyword, notalnum)
            .AddToken("continue", "continue", TokenType.Keyword, notalnum)
            .AddToken("goto", "goto", TokenType.Keyword, notalnum)
            .AddToken("do", "do", TokenType.Keyword, notalnum)
            .AddToken("end", "end", TokenType.Keyword, notalnum)
            .AddToken("while", "while", TokenType.Keyword, notalnum)
            .AddToken("repeat", "repeat", TokenType.Keyword, notalnum)
            .AddToken("until", "until", TokenType.Keyword, notalnum)
            .AddToken("if", "if", TokenType.Keyword, notalnum)
            .AddToken("then", "then", TokenType.Keyword, notalnum)
            .AddToken("elseif", "elseif", TokenType.Keyword, notalnum)
            .AddToken("else", "else", TokenType.Keyword, notalnum)
            .AddToken("for", "for", TokenType.Keyword, notalnum)
            .AddToken("in", "in", TokenType.Keyword, notalnum)
            .AddToken("function", "function", TokenType.Keyword, notalnum)
            .AddToken("local", "local", TokenType.Keyword, notalnum)
            .AddToken("return", "return", TokenType.Keyword, notalnum)
            .AddToken("nil", "nil", TokenType.Keyword, notalnum)
            .AddToken("true", "true", TokenType.Keyword, notalnum)
            .AddToken("false", "false", TokenType.Keyword, notalnum);

            #endregion Keywords
        }
Exemplo n.º 5
0
 public GLuaLexer(Stream input) : this(CInterop.ReadToEndAsASCII(input))
 {
 }