Esempio n. 1
0
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            while (true)
            {
                //Find next position
                var newPos = source.Text.IndexOfAny(_stopChars, source.PreviewPosition + 1);
                //we either didn't find it
                if (newPos == -1)
                {
                    return(source.CreateErrorToken(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.ErrNoEndForRegex));// "No end symbol for regex literal."
                }
                source.PreviewPosition = newPos;
                if (source.PreviewChar != EndSymbol)
                {
                    //we hit CR or LF, this is an error
                    return(source.CreateErrorToken(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.ErrNoEndForRegex));
                }
                if (!CheckEscaped(source))
                {
                    break;
                }
            }
            source.PreviewPosition++;                                               //move after end symbol
            //save pattern length, we will need it
            var patternLen = source.PreviewPosition - source.Location.Position - 2; //exclude start and end symbol
            //read switches and turn them into options
            RegexOptions options  = RegexOptions.None;
            var          switches = string.Empty;

            while (ReadSwitch(source, ref options))
            {
                if (IsSet(RegexTermOptions.UniqueSwitches) && switches.Contains(source.PreviewChar))
                {
                    return(source.CreateErrorToken(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.ErrDupRegexSwitch, source.PreviewChar)); // "Duplicate switch '{0}' for regular expression"
                }
                switches += source.PreviewChar.ToString();
                source.PreviewPosition++;
            }
            //check following symbol
            if (!IsSet(RegexTermOptions.AllowLetterAfter))
            {
                var currChar = source.PreviewChar;
                if (char.IsLetter(currChar) || currChar == '_')
                {
                    return(source.CreateErrorToken(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.ErrInvRegexSwitch, currChar)); // "Invalid switch '{0}' for regular expression"
                }
            }
            var token = source.CreateToken(this.OutputTerminal);
            //we have token, now what's left is to set its Value field. It is either pattern itself, or Regex instance
            string pattern = token.Text.Substring(1, patternLen); //exclude start and end symbol
            object value   = pattern;

            if (IsSet(RegexTermOptions.CreateRegExObject))
            {
                value = new Regex(pattern, options);
            }
            token.Value   = value;
            token.Details = switches; //save switches in token.Details
            return(token);
        }
Esempio n. 2
0
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            Token result;

            if (context.VsLineScanState.Value != 0)
            {
                byte level = context.VsLineScanState.TokenSubType;
                result = CompleteMatch(context, source, level);
            }
            else
            {
                //we are starting from scratch
                byte level = 0;
                if (!BeginMatch(context, source, ref level))
                {
                    return(null);
                }

                result = CompleteMatch(context, source, level);
            }

            if (result != null)
            {
                return(result);
            }

            if (context.Mode == ParseMode.VsLineScan)
            {
                return(CreateIncompleteToken(context, source));
            }

            return(source.CreateErrorToken("Unclosed comment block"));
        }
Esempio n. 3
0
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            Token token;

            if (context.VsLineScanState.Value != 0)
            {
                byte tokenSubType = context.VsLineScanState.TokenSubType;
                token = CompleteMatch(context, source, tokenSubType);
            }
            else
            {
                byte level = 0;
                if (!BeginMatch(context, source, ref level))
                {
                    return(null);
                }
                token = CompleteMatch(context, source, level);
            }
            if (token != null)
            {
                return(token);
            }
            if (context.Mode == ParseMode.VsLineScan)
            {
                return(CreateIncompleteToken(context, source));
            }
            return(source.CreateErrorToken("Unclosed comment block", new object[0]));
        }
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            Token result;
            if (context.VsLineScanState.Value != 0)
            {
                byte commentLevel = context.VsLineScanState.TokenSubType;
                result = CompleteMatch(context, source, commentLevel);
            }
            else
            {
                //we are starting from scratch
                byte commentLevel = 0;
                if (!BeginMatch(context, source, ref commentLevel))
                    return null;

                result = CompleteMatch(context, source, commentLevel);
            }

            if (result != null)
                return result;

            if (context.Mode == ParseMode.VsLineScan)
                return CreateIncompleteToken(context, source);

            return source.CreateErrorToken("Unclosed comment block");
        }
Esempio n. 5
0
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            Token result;

            if (context.VsLineScanState.Value != 0)
            {
                // we are continuing in line mode - restore internal env (none in this case)
                context.VsLineScanState.Value = 0;
            }
            else
            {
                //we are starting from scratch
                if (!BeginMatch(context, source))
                {
                    return(null);
                }
            }
            result = CompleteMatch(context, source);
            if (result != null)
            {
                return(result);
            }
            //if it is LineComment, it is ok to hit EOF without final line-break; just return all until end.
            if (_isLineComment)
            {
                return(source.CreateToken(this.OutputTerminal));
            }
            if (context.Mode == ParseMode.VsLineScan)
            {
                return(CreateIncompleteToken(context, source));
            }
            return(source.CreateErrorToken(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.ErrUnclosedComment));
        }
Esempio n. 6
0
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            // Quick check
            var lookAhead  = source.PreviewChar;
            var startIndex = _startSymbolsFirsts.IndexOf(lookAhead);

            if (startIndex < 0)
            {
                return(null);
            }

            // Match start symbols
            if (!BeginMatch(source, startIndex, lookAhead))
            {
                return(null);
            }

            // Match NewLine
            var result = CompleteMatch(source);

            if (result != null)
            {
                return(result);
            }

            // Report an error
            return(source.CreateErrorToken(Resources.ErrNewLineExpected));
        }
Esempio n. 7
0
 public override Token TryMatch(ParsingContext context, ISourceStream source) {
   try {
     var textValue = ReadBody(context, source);
     if (textValue == null) return null; 
     var value = ConvertValue(context, textValue);
     return source.CreateToken(this.OutputTerminal, value);
   } catch(Exception ex) {
     //we throw exception in DsvLiteral when we cannot find a closing quote for quoted value
     return source.CreateErrorToken(ex.Message);
   }
 }//method
Esempio n. 8
0
 public override Token TryMatch(ParsingContext context, ISourceStream source)
 {
     try {
         var textValue = ReadBody(context, source);
         if (textValue == null)
         {
             return(null);
         }
         var value = ConvertValue(context, textValue);
         return(source.CreateToken(this.OutputTerminal, value));
     } catch (Exception ex) {
         //we throw exception in DsvLiteral when we cannot find a closing quote for quoted value
         return(source.CreateErrorToken(ex.Message));
     }
 }//method
Esempio n. 9
0
        private Token TryMatchContentSimple(ParsingContext context, ISourceStream source)
        {
            var startPos = source.PreviewPosition;
            int p        = source.Text.IndexOf(_singleTerminator, startPos, Grammar.StringComparisonMode);

            if (p < 0 && IsSet(FreeTextOptions.AllowEof))
            {
                p = source.Text.Length;
            }
            if (p < 0)
            {
                return(source.CreateErrorToken(Resources.ErrFreeTextNoEndTag, _singleTerminator));
            }
            var tokenText = source.Text.Substring(startPos, p - startPos);

            return(source.CreateToken(this.OutputTerminal, tokenText));
        }
 public override Token TryMatch(ParsingContext context, ISourceStream source) {
   Token result;
   if (context.VsLineScanState.Value != 0) {
     // we are continuing in line mode - restore internal env (none in this case)
     context.VsLineScanState.Value = 0;
   } else {
     //we are starting from scratch
     if (!BeginMatch(context, source)) return null;
   }
   result = CompleteMatch(context, source);
   if (result != null) return result;
   //if it is LineComment, it is ok to hit EOF without final line-break; just return all until end.
   if (_isLineComment)
     return source.CreateToken(this.OutputTerminal);
   if (context.Mode == ParseMode.VsLineScan)
     return CreateIncompleteToken(context, source);
   return source.CreateErrorToken(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.ErrUnclosedComment);
 }
Esempio n. 11
0
    public override Token TryMatch(ParsingContext context, ISourceStream source) {
      // Quick check
      var lookAhead = source.PreviewChar;
      var startIndex = _startSymbolsFirsts.IndexOf(lookAhead);
      if (startIndex < 0)
        return null;

      // Match start symbols
      if (!BeginMatch(source, startIndex, lookAhead))
        return null;

      // Match NewLine
      var result = CompleteMatch(source);
      if (result != null)
        return result;

      // Report an error
      return source.CreateErrorToken(Resources.ErrNewLineExpected);
    }
Esempio n. 12
0
        public override Token TryMatch(ParsingContext context, ISourceStream source)
        {
            Token token;

            //Try quick parse first, but only if we're not continuing
            if (context.VsLineScanState.Value == 0)
            {
                token = QuickParse(context, source);
                if (token != null)
                {
                    return(token);
                }
                source.PreviewPosition = source.Location.Position; //revert the position
            }

            CompoundTokenDetails details = new CompoundTokenDetails();

            InitDetails(context, details);

            if (context.VsLineScanState.Value == 0)
            {
                ReadPrefix(source, details);
            }
            if (!ReadBody(source, details))
            {
                return(null);
            }
            if (details.Error != null)
            {
                return(source.CreateErrorToken(details.Error));
            }
            if (details.IsPartial)
            {
                details.Value = details.Body;
            }
            else
            {
                ReadSuffix(source, details);

                if (!ConvertValue(details))
                {
                    if (string.IsNullOrEmpty(details.Error))
                    {
                        details.Error = Resources.ErrInvNumber;
                    }
                    return(source.CreateErrorToken(details.Error)); // "Failed to convert the value: {0}"
                }
            }
            token = CreateToken(context, source, details);

            if (details.IsPartial)
            {
                //Save terminal state so we can continue
                context.VsLineScanState.TokenSubType  = (byte)details.SubTypeIndex;
                context.VsLineScanState.TerminalFlags = (short)details.Flags;
                context.VsLineScanState.TerminalIndex = this.MultilineIndex;
            }
            else
            {
                context.VsLineScanState.Value = 0;
            }
            return(token);
        }
Esempio n. 13
0
 private Token TryMatchContentSimple(ParsingContext context, ISourceStream source)
 {
     var startPos = source.PreviewPosition;
       int p = source.Text.IndexOf(_singleTerminator, startPos, Grammar.StringComparisonMode);
       if (p < 0 && IsSet(FreeTextOptions.AllowEof))
     p = source.Text.Length;
       if (p < 0)
     return source.CreateErrorToken(Resources.ErrFreeTextNoEndTag, _singleTerminator);
       var tokenText = source.Text.Substring(startPos, p - startPos);
       return source.CreateToken(this.OutputTerminal, tokenText);
 }