Beispiel #1
0
        /// <summary>
        /// Consume the token
        /// </summary>
        public void ConsumeToken()
        {
            if (currentInput == null)
            {
                return;
            }

            bool result;

            tokenBeginPosition = new PositionStruct(currentInput.CurrentLine, currentInput.CurrentColumn, currentInput.CurrentOffset, currentInput.Filename);

            do
            {
                if (popInputAtNextConsume)
                {
                    PopAndCloseInput();
                    popInputAtNextConsume = false;
                }
                do
                {
                    result = lexerFunc();
                } while (result == false);
                if (CurrentTokenType == LexerTokenType.Eof)
                {
                    popInputAtNextConsume = true;
                    if (inputStack.Count > 0)
                    {
                        // report as empty NewLine token
                        currentToken = new Token(
                            LexerTokenType.NewLine, tokenBeginPosition, tokenBeginPosition, string.Empty, tokenBeginPosition.Filename);
                    }
                }
            } while (result == false);
        }
 /// <summary>
 /// Class for a custom exception for the Batch Parser
 /// </summary>
 public BatchParserException(ErrorCode errorCode, Token token, string message)
     : base(message)
 {
     this.errorCode = errorCode;
     begin          = token.Begin;
     end            = token.End;
     text           = token.Text;
     tokenType      = token.TokenType;
 }
Beispiel #3
0
        private static Token GetSubToken(Token token, int startOffset, int endOffset, LexerTokenType?newTokenType = null)
        {
            LexerTokenType tokenType = newTokenType.HasValue ? newTokenType.Value : token.TokenType;
            string         text = token.Text.Substring(startOffset, endOffset - startOffset);
            string         filename = token.Begin.Filename;
            PositionStruct beginPos, endPos;

            int beginLine, beginColumn;

            LineInfo.CalculateLineColumnForOffset(token, startOffset, out beginLine, out beginColumn);
            beginPos = new PositionStruct(beginLine, beginColumn, token.Begin.Offset + startOffset, filename);

            int endLine, endColumn;

            LineInfo.CalculateLineColumnForOffset(token, endOffset, out endLine, out endColumn);
            endPos = new PositionStruct(endLine, endColumn, token.Begin.Offset + endOffset, filename);

            return(new Token(tokenType, beginPos, endPos, text, filename));
        }
Beispiel #4
0
        private void AddTokenToStringBuffer()
        {
            Token token = LookaheadToken;

            if (token.TokenType != LexerTokenType.Comment)
            {
                // All variable references must be valid
                CheckVariableReferences(token);
            }
            if (token.TokenType == LexerTokenType.NewLine && token.Text.Length == 0)
            {
                // Add "virtual" token representing new line that was not in original file
                PositionStruct beginPos = token.Begin;
                PositionStruct endPos   = new PositionStruct(beginPos.Line + 1, 1, beginPos.Offset, beginPos.Filename);

                tokenBuffer.Add(new Token(LexerTokenType.NewLine, beginPos, endPos, Environment.NewLine, beginPos.Filename));
            }
            else
            {
                tokenBuffer.Add(token);
            }
        }
Beispiel #5
0
        internal string ResolveVariables(Token inputToken, int offset, List <VariableReference> variableRefs)
        {
            List <VariableReference> variableRefsLocal = new List <VariableReference>();

            AddVariableReferences(inputToken, offset, variableRefsLocal);

            string inputString = inputToken.Text;

            if (variableRefsLocal.Count == 0)
            {
                // no variable references to substitute
                return(inputString);
            }

            StringBuilder  sb          = new StringBuilder();
            int            lastChar    = 0;
            PositionStruct?variablePos = null;

            foreach (VariableReference reference in variableRefsLocal)
            {
                int line;
                int column;

                if (variablePos != null)
                {
                    LineInfo.CalculateLineColumnForOffset(inputToken.Text, reference.Start - offset,
                                                          variablePos.Value.Offset, variablePos.Value.Line, variablePos.Value.Column,
                                                          out line, out column);
                }
                else
                {
                    LineInfo.CalculateLineColumnForOffset(inputToken, reference.Start - offset, out line, out column);
                }

                variablePos = new PositionStruct(
                    line: line,
                    column: column,
                    offset: reference.Start - offset,
                    filename: inputToken.Filename);
                string value = variableResolver.GetVariable(variablePos.Value, reference.VariableName);
                if (value == null)
                {
                    // Undefined variable
                    if (ThrowOnUnresolvedVariable == true)
                    {
                        RaiseError(ErrorCode.VariableNotDefined, inputToken,
                                   string.Format(CultureInfo.CurrentCulture, SR.BatchParser_VariableNotDefined, reference.VariableName));
                    }
                    continue;
                }

                reference.VariableValue = value;
                sb.Append(inputToken.Text, lastChar, reference.Start - offset - lastChar);
                sb.Append(value);
                lastChar = reference.Start - offset + reference.Length;
            }
            if (lastChar != inputString.Length)
            {
                sb.Append(inputString, lastChar, inputString.Length - lastChar);
            }
            if (variableRefs != null)
            {
                variableRefs.AddRange(variableRefsLocal);
            }
            return(sb.ToString());
        }