Beispiel #1
0
 public static bool IsDoubleQuote(LexerContext context)
 {
     return(!context.IsEnded() &&
            !context.IsLast() &&
            context.GetCurrentChar().Equals('\'') &&
            context.GetNextChar().Equals('\''));
 }
Beispiel #2
0
 internal Token(LexerContext ctx, int index, TokenType type, string value)
 {
     this.ctx = ctx;
     Index    = index;
     Type     = type;
     Value    = value;
 }
Beispiel #3
0
 public static bool IsOctalNumberBegin(LexerContext context)
 {
     return(context.GetCurrentChar().Equals('&') &&
            !context.IsEnded() &&
            !context.IsLast() &&
            OctalDigits.Contains(context.GetNextChar()));
 }
Beispiel #4
0
 public static bool IsDelphiCommentBegin(LexerContext context)
 {
     return(!context.IsEnded() &&
            !context.IsLast() &&
            context.GetCurrentChar().Equals('/') &&
            context.GetNextChar().Equals('/'));
 }
Beispiel #5
0
 public static bool IsOldStyleCommentEnd(LexerContext context)
 {
     return(!context.IsEnded() &&
            !context.IsLast() &&
            context.GetCurrentChar().Equals('*') &&
            context.GetNextChar().Equals(')'));
 }
Beispiel #6
0
 public static bool IsBinaryNumberBegin(LexerContext context)
 {
     return(context.GetCurrentChar().Equals('%') &&
            !context.IsEnded() &&
            !context.IsLast() &&
            HexadecimalDigits.Contains(context.GetNextChar()));
 }
Beispiel #7
0
 public static bool IsNumberBegin(LexerContext context)
 {
     return(IsDecimalNumberBegin(context) ||
            IsHexNumberBegin(context) ||
            IsOctalNumberBegin(context) ||
            IsBinaryNumberBegin(context));
 }
Beispiel #8
0
 public Token Lex(ISource source, int start)
 {
     using (var context = new LexerContext(source, start))
     {
         return(context.GetToken());
     }
 }
Beispiel #9
0
 public static bool IsStringBegin(LexerContext context)
 {
     return(!context.IsEnded() &&
            !context.IsLast() &&
            (
                context.GetCurrentChar().Equals('\'') && IsStringSymbol(context.GetNextChar()) ||
                context.GetCurrentChar().Equals('#') && Char.IsDigit(context.GetNextChar())
            ));
 }
Beispiel #10
0
 /// <summary>
 /// 指示当前对象是否等于同一类型的另一个对象。
 /// </summary>
 /// <param name="other">与此对象进行比较的对象。</param>
 /// <returns>如果当前对象等于 <paramref name="other"/>,
 /// 则为 <c>true</c>;否则为 <c>false</c>。</returns>
 /// <overloads>
 /// <summary>
 /// 指示当前对象是否等于另一个对象。
 /// </summary>
 /// </overloads>
 public bool Equals(LexerContext other)
 {
     if (object.ReferenceEquals(other, this))
     {
         return(true);
     }
     if (this.index != other.index)
     {
         return(false);
     }
     if (this.contextType != other.contextType)
     {
         return(false);
     }
     return(this.label == other.label);
 }
Beispiel #11
0
		/// <summary>
		/// 指示当前对象是否等于同一类型的另一个对象。
		/// </summary>
		/// <param name="other">与此对象进行比较的对象。</param>
		/// <returns>如果当前对象等于 <paramref name="other"/>,
		/// 则为 <c>true</c>;否则为 <c>false</c>。</returns>
		/// <overloads>
		/// <summary>
		/// 指示当前对象是否等于另一个对象。
		/// </summary>
		/// </overloads>
		public bool Equals(LexerContext other)
		{
			if (object.ReferenceEquals(other, this))
			{
				return true;
			}
			if (this.index != other.index)
			{
				return false;
			}
			if (this.contextType != other.contextType)
			{
				return false;
			}
			return this.label == other.label;
		}
Beispiel #12
0
 /// <summary>
 /// Restore a previously saved Lexer context.
 /// https://github.com/TypeCobolTeam/TypeCobol/issues/1000
 /// </summary>
 public static void RestoreLexerContext()
 {
     if (UseStack.Count > 0)
     {
         LexerContext ctx = UseStack.Pop();
         System.Console.In.Close();
         System.Console.SetIn(ctx.In);
         CurrentFile       = ctx.CurrentFile;
         current_line      = ctx.CurrentLine;
         current_position  = ctx.CurrentPosition;
         absolute_position = ctx.AbsolutePosition;
         next_char         = ctx.NextChar;
         next_char2        = ctx.NextChar2;
         next_char3        = ctx.NextChar3;
         next_char4        = ctx.NextChar4;
     }
 }
Beispiel #13
0
        /// <summary>
        /// Switch the current lexer context to a new one.
        /// https://github.com/TypeCobolTeam/TypeCobol/issues/1000
        /// </summary>
        /// <param name="filePath">The new file path</param>
        /// <param name="sr">The new stream reader</param>
        public static void SwitchLexerContext(string filePath, System.IO.StreamReader sr)
        {
            LexerContext ctx = new LexerContext();

            ctx.FilePath         = filePath;
            ctx.In               = System.Console.In;
            ctx.CurrentFile      = CurrentFile;
            ctx.CurrentLine      = current_line;
            ctx.CurrentPosition  = current_position;
            ctx.AbsolutePosition = absolute_position;
            ctx.NextChar         = next_char;
            ctx.NextChar2        = next_char2;
            ctx.NextChar3        = next_char3;
            ctx.NextChar4        = next_char4;
            UseStack.Push(ctx);
            System.Console.SetIn(sr);
            current_line      = 1;
            current_position  = 1;
            absolute_position = 1;
            CurrentFile       = filePath;
            InitLookaheads();
        }
Beispiel #14
0
 public static bool IsBinaryDigit(LexerContext context)
 {
     return(BinaryDigits.Contains(context.GetCurrentChar()));
 }
Beispiel #15
0
 public static bool IsHexadecimalDigit(LexerContext context)
 {
     return(HexadecimalDigits.Contains(context.GetCurrentChar()));
 }
Beispiel #16
0
 public static bool IsDecimalDigit(LexerContext context)
 {
     return(Char.IsDigit(context.GetCurrentChar()));
 }
 /// <summary>
 /// Changes the lexer's current context.
 /// </summary>
 /// <param name="context">The lexer's new context.</param>
 private static void ChangeContext(LexerContext context)
 {
     currentContext = context ?? cachedLexerContext_Default;
     currentContext.Initialize();
 }
Beispiel #18
0
 public static bool IsCommentBegin(LexerContext context)
 {
     return(IsOldStyleCommentBegin(context) ||
            IsTurboPascalCommentBegin(context) ||
            IsDelphiCommentBegin(context));
 }
Beispiel #19
0
 public static bool IsTurboPascalCommentEnd(LexerContext context)
 {
     return(!context.IsEnded() &&
            context.GetCurrentChar().Equals('}'));
 }
Beispiel #20
0
 public static bool IsIdentifierBegin(LexerContext context)
 {
     return(context.GetCurrentChar() == '_' || Char.IsLetter(context.GetCurrentChar()));
 }
Beispiel #21
0
 public static bool IsSpecialPair(LexerContext context)
 {
     return(SpecialPairs.Contains(context.GetCharPair()));
 }
Beispiel #22
0
        public Token Lex(ISource source, int start)
        {
            var context = new LexerContext(source, start, Cache);

            return(context.GetToken());
        }
Beispiel #23
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /** The actual routine to return one Symbol.  This is normally called from
         *  next_token(), but for debugging purposes can be called indirectly from
         *  debug_next_token().
         */
        protected static Symbol real_next_token()
        {
            int sym_num;

            for (;;)
            {
                /* look for white space */
                if (next_char == ' ' || next_char == '\t' || next_char == '\n' ||
                    next_char == '\f' || next_char == '\r')
                {
                    /* advance past it and try the next character */
                    advance();
                    continue;
                }

                /* look for a single character symbol */
                sym_num = find_single_char(next_char);
                if (sym_num != -1)
                {
                    /* found one -- advance past it and return a Symbol for it */
                    advance();
                    return(new Symbol(sym_num));
                }

                /* look for : or ::= */
                if (next_char == ':')
                {
                    /* if we don't have a second ':' return COLON */
                    if (next_char2 != ':')
                    {
                        advance();
                        return(new Symbol(sym.COLON));
                    }

                    /* move forward and look for the '=' */
                    advance();
                    if (next_char2 == '=')
                    {
                        advance(); advance();
                        return(new Symbol(sym.COLON_COLON_EQUALS));
                    }
                    else
                    {
                        /* return just the colon (already consumed) */
                        return(new Symbol(sym.COLON));
                    }
                }

                /* find a "%prec" string and return it.  otherwise, a '%' was found,
                 * which has no right being in the specification otherwise */
                if (next_char == '%')
                {
                    advance();
                    if ((next_char == 'p') && (next_char2 == 'r') && (next_char3 == 'e') &&
                        (next_char4 == 'c'))
                    {
                        advance();
                        advance();
                        advance();
                        advance();
                        return(new Symbol(sym.PERCENT_PREC));
                    }
                    else
                    {
                        emit_error("Found extraneous percent sign");
                    }
                }

                /* look for a comment */
                if (next_char == '/' && (next_char2 == '*' || next_char2 == '/'))
                {
                    /* swallow then continue the scan */
                    swallow_comment();
                    continue;
                }

                /* look for start of code string */
                if (next_char == '{' && next_char2 == ':')
                {
                    return(do_code_string());
                }

                /* look for an id or keyword */
                if (id_start_char(next_char))
                {
                    return(do_id());
                }

                if (next_char == '#' && current_position == 1 && (next_char2 == 'u') && (next_char3 == 's') &&
                    (next_char4 == 'e'))
                {  //https://github.com/TypeCobolTeam/TypeCobol/issues/1000
                   //#use directive must appears as the first character in the line.
                   //Read the whole line after the #use directive and trim it as being the file path to be included.
                    string useLine = System.Console.In.ReadLine();
                    current_line += 1;
                    lexer.InitLookaheads();
                    return(LexerContext.HandleUseDirective(useLine?.Trim()));
                }

                /* look for EOF */
                if (next_char == EOF_CHAR)
                {
                    return(new Symbol(sym.EOF));
                }

                /* if we get here, we have an unrecognized character */
                emit_warn("Unrecognized character '" +
                          next_char + "'(" + next_char +
                          ") -- ignored");

                /* advance past it */
                advance();
            }
        }
Beispiel #24
0
 public static bool IsSpecial(LexerContext context)
 {
     return(SpecialCharacters.Contains(context.GetCurrentChar()));
 }