Example #1
0
        static IEnumerable <string> ParseLine(string Line)
        {
            ParseBuilder.Length = 0;
            //Line = Line.Replace("=", " = ").Replace(";", " ; ").Replace("\r", "").Replace("\n", "");
            bool InsideQuote = false;

            const string Symbols     = "+-=;";
            const string SkipSymbols = " ,";

            char LastChr = (char)0;

            foreach (var Chr in Line)
            {
                if (!InsideQuote && SkipSymbols.Contains(Chr))
                {
                    if (ParseBuilder.Length > 0)
                    {
                        yield return(ParseBuilder.ToString());

                        ParseBuilder.Length = 0;
                    }
                }
                else if (!InsideQuote && Symbols.Contains(Chr))
                {
                    if (ParseBuilder.Length > 0)
                    {
                        yield return(ParseBuilder.ToString());

                        ParseBuilder.Length = 0;
                    }

                    yield return(Chr.ToString());
                }
                else if (Chr == '"' && LastChr != '\\')
                {
                    InsideQuote = !InsideQuote;
                }
                else if (Chr == '"' && LastChr == '\\')
                {
                    ParseBuilder.Length--;
                    ParseBuilder.Append('"');
                }
                else
                {
                    ParseBuilder.Append(Chr);
                }

                LastChr = Chr;
            }

            if (ParseBuilder.Length > 0)
            {
                yield return(ParseBuilder.ToString());
            }
        }
Example #2
0
        public string PrintGrammar()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("//Terminals:");
            foreach (Symbol s in GetTerminals())
            {
                Symbol skip = SkipSymbols.Find(s.Name);
                if (skip != null)
                {
                    sb.Append("[Skip] ");
                }
                sb.AppendLine(s.PrintProduction());
            }

            sb.AppendLine("\r\n//Production lines:");
            foreach (Symbol s in GetNonTerminals())
            {
                sb.AppendLine(s.PrintProduction());
            }
            return(sb.ToString());
        }
Example #3
0
        /// <summary>
        /// Advances past any whitespace and comments in the code.
        /// </summary>
        /// <param name="skip">
        /// Indicates the types of tokens to advance past.
        /// </param>
        /// <param name="parentReference">
        /// The parent code part.
        /// </param>
        private void AdvanceToNextCodeSymbol(SkipSymbols skip, Reference<ICodePart> parentReference)
        {
            Param.Ignore(skip);
            Param.AssertNotNull(parentReference, "parentReference");

            Symbol symbol = this.symbols.Peek(1);
            while (symbol != null)
            {
                if (symbol.SymbolType == SymbolType.WhiteSpace && (skip & SkipSymbols.WhiteSpace) != 0)
                {
                    this.tokens.Add(new Whitespace(symbol.Text, symbol.Location, parentReference, this.symbols.Generated));
                    this.symbols.Advance();
                }
                else if (symbol.SymbolType == SymbolType.EndOfLine && (skip & SkipSymbols.EndOfLine) != 0)
                {
                    this.tokens.Add(new CsToken(symbol.Text, CsTokenType.EndOfLine, symbol.Location, parentReference, this.symbols.Generated));
                    this.symbols.Advance();
                }
                else if (symbol.SymbolType == SymbolType.SingleLineComment && (skip & SkipSymbols.SingleLineComment) != 0)
                {
                    this.tokens.Add(new CsToken(symbol.Text, CsTokenType.SingleLineComment, symbol.Location, parentReference, this.symbols.Generated));
                    this.symbols.Advance();
                }
                else if (symbol.SymbolType == SymbolType.MultiLineComment && (skip & SkipSymbols.MultiLineComment) != 0)
                {
                    this.tokens.Add(new CsToken(symbol.Text, CsTokenType.MultiLineComment, symbol.Location, parentReference, this.symbols.Generated));
                    this.symbols.Advance();
                }
                else if (symbol.SymbolType == SymbolType.PreprocessorDirective && (skip & SkipSymbols.Preprocessor) != 0)
                {
                    this.tokens.Add(this.GetPreprocessorDirectiveToken(symbol, parentReference, this.symbols.Generated));
                    this.symbols.Advance();
                }
                else if (symbol.SymbolType == SymbolType.XmlHeaderLine && (skip & SkipSymbols.XmlHeader) != 0)
                {
                    this.tokens.Add(new CsToken(symbol.Text, CsTokenType.XmlHeaderLine, symbol.Location, parentReference, this.symbols.Generated));
                    this.symbols.Advance();
                }
                else
                {
                    break;
                }

                symbol = this.symbols.Peek(1);
            }
        }
Example #4
0
        /// <summary>
        /// Advances to the next code symbol and returns it.
        /// </summary>
        /// <param name="skip">
        /// Indicates the types of symbols to skip past.
        /// </param>
        /// <param name="parentReference">
        /// The parent code part.
        /// </param>
        /// <param name="allowNull">
        /// If true, indicates that this method is allowed to return a null symbol, if there are no
        /// more symbols in the document. If false, the method will throw an exception if it is unable to get another symbol.
        /// </param>
        /// <returns>
        /// Returns the next code symbol.
        /// </returns>
        /// <exception cref="SyntaxException">
        /// Will be thrown if there are no more symbols in the document.
        /// </exception>
        private Symbol GetNextSymbol(SkipSymbols skip, Reference<ICodePart> parentReference, bool allowNull)
        {
            Param.Ignore(skip);
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(allowNull);

            this.AdvanceToNextCodeSymbol(skip, parentReference);

            Symbol symbol = this.symbols.Peek(1);
            if (symbol == null && !allowNull)
            {
                throw this.CreateSyntaxException();
            }

            return symbol;
        }
Example #5
0
        /// <summary>
        /// Advances to the next code symbol and returns it.
        /// </summary>
        /// <param name="skip">
        /// Indicates the types of symbols to skip past.
        /// </param>
        /// <param name="parentReference">
        /// The parent code part.
        /// </param>
        /// <returns>
        /// Returns the next code symbol.
        /// </returns>
        /// <exception cref="SyntaxException">
        /// Will be thrown if there are no more symbols in the document.
        /// </exception>
        private Symbol GetNextSymbol(SkipSymbols skip, Reference<ICodePart> parentReference)
        {
            Param.Ignore(skip);
            Param.AssertNotNull(parentReference, "parentReference");

            return this.GetNextSymbol(skip, parentReference, false);
        }
Example #6
0
        /// <summary>
        /// Advances to the next code symbol and returns it.
        /// </summary>
        /// <param name="symbolType">
        /// The expected type of the symbol.
        /// </param>
        /// <param name="skip">
        /// The types of symbols to skip past.
        /// </param>
        /// <param name="parentReference">
        /// The parent code part.
        /// </param>
        /// <returns>
        /// Returns the next code symbol.
        /// </returns>
        /// <exception cref="SyntaxException">
        /// Will be thrown if there are no more symbols in the document or if the 
        /// next symbol is not of the expected type.
        /// </exception>
        private Symbol GetNextSymbol(SymbolType symbolType, SkipSymbols skip, Reference<ICodePart> parentReference)
        {
            Param.Ignore(symbolType, skip);
            Param.AssertNotNull(parentReference, "parentReference");

            Symbol symbol = this.GetNextSymbol(skip, parentReference);
            if (symbol.SymbolType != symbolType)
            {
                throw this.CreateSyntaxException();
            }

            return symbol;
        }
Example #7
0
 private Symbol GetNextSymbol(SymbolType symbolType, SkipSymbols skip)
 {
     Symbol nextSymbol = this.GetNextSymbol(skip);
     if (nextSymbol.SymbolType != symbolType)
     {
         throw this.CreateSyntaxException();
     }
     return nextSymbol;
 }
Example #8
0
 private Symbol GetNextSymbol(SkipSymbols skip, bool allowNull)
 {
     this.AdvanceToNextCodeSymbol(skip);
     Symbol symbol = this.symbols.Peek(1);
     if ((symbol == null) && !allowNull)
     {
         throw this.CreateSyntaxException();
     }
     return symbol;
 }
Example #9
0
 private Symbol GetNextSymbol(SkipSymbols skip)
 {
     return this.GetNextSymbol(skip, false);
 }
Example #10
0
 private void AdvanceToNextCodeSymbol(SkipSymbols skip)
 {
     for (Symbol symbol = this.symbols.Peek(1); symbol != null; symbol = this.symbols.Peek(1))
     {
         if ((symbol.SymbolType == SymbolType.WhiteSpace) && ((skip & SkipSymbols.WhiteSpace) != SkipSymbols.None))
         {
             this.tokens.Add(new Whitespace(symbol.Text, symbol.Location, this.symbols.Generated));
             this.symbols.Advance();
         }
         else if ((symbol.SymbolType == SymbolType.EndOfLine) && ((skip & SkipSymbols.EndOfLine) != SkipSymbols.None))
         {
             this.tokens.Add(new CsToken(symbol.Text, CsTokenType.EndOfLine, symbol.Location, this.symbols.Generated));
             this.symbols.Advance();
         }
         else if ((symbol.SymbolType == SymbolType.SingleLineComment) && ((skip & SkipSymbols.SingleLineComment) != SkipSymbols.None))
         {
             this.tokens.Add(new CsToken(symbol.Text, CsTokenType.SingleLineComment, symbol.Location, this.symbols.Generated));
             this.symbols.Advance();
         }
         else if ((symbol.SymbolType == SymbolType.MultiLineComment) && ((skip & SkipSymbols.MultiLineComment) != SkipSymbols.None))
         {
             this.tokens.Add(new CsToken(symbol.Text, CsTokenType.MultiLineComment, symbol.Location, this.symbols.Generated));
             this.symbols.Advance();
         }
         else if ((symbol.SymbolType == SymbolType.PreprocessorDirective) && ((skip & SkipSymbols.Preprocessor) != SkipSymbols.None))
         {
             this.tokens.Add(this.GetPreprocessorDirectiveToken(symbol, this.symbols.Generated));
             this.symbols.Advance();
         }
         else
         {
             if ((symbol.SymbolType != SymbolType.XmlHeaderLine) || ((skip & SkipSymbols.XmlHeader) == SkipSymbols.None))
             {
                 break;
             }
             this.tokens.Add(new CsToken(symbol.Text, CsTokenType.XmlHeaderLine, symbol.Location, this.symbols.Generated));
             this.symbols.Advance();
         }
     }
 }
Example #11
0
        /// <summary>
        /// Returns the next code symbol without allocating any tokens or changing the current symbol index.
        /// </summary>
        /// <param name="skip">Indicates the types of symbols to skip past.</param>
        /// <param name="allowNull">If true, indicates that this method is allowed to return a null symbol, if there are no
        /// more symbols in the document. If false, the method will throw an exception if it is unable to get another symbol.</param>
        /// <returns>Returns the next code symbol.</returns>
        /// <exception cref="SyntaxException">Will be thrown if there are no more symbols in the document.</exception>
        private Symbol PeekNextSymbol(SkipSymbols skip, bool allowNull)
        {
            Param.Ignore(skip);
            Param.Ignore(allowNull);

            Symbol symbol = this.PeekToNextCodeSymbol(skip);
            if (symbol == null && !allowNull)
            {
                throw this.CreateSyntaxException();
            }

            return symbol;
        }
Example #12
0
        /// <summary>
        /// Returns the next code symbol without allocating any tokens or changing the current symbol index.
        /// </summary>
        /// <param name="skip">Indicates the types of symbols to skip past.</param>
        /// <returns>Returns the next code symbol.</returns>
        /// <exception cref="SyntaxException">Will be thrown if there are no more symbols in the document.</exception>
        private Symbol PeekNextSymbol(SkipSymbols skip)
        {
            Param.Ignore(skip);

            return this.PeekNextSymbol(skip, false);
        }
Example #13
0
        /// <summary>
        /// Advances past any whitespace and comments in the code.
        /// </summary>
        /// <param name="skip">Indicates the types of tokens to advance past.</param>
        /// <returns>Returns the next symbol.</returns>
        private Symbol PeekToNextCodeSymbol(SkipSymbols skip)
        {
            Param.Ignore(skip);

            int index = 1;
            Symbol symbol = this.symbols.Peek(index);
            
            // NOTE: If the list of symbols checked here is changed, you must also make the same corresponding change in the AdvanceToNextCodeSymbol method.
            while ((symbol != null) &&
                (((symbol.SymbolType == SymbolType.WhiteSpace) && (skip & SkipSymbols.WhiteSpace) != 0) ||
                 ((symbol.SymbolType == SymbolType.EndOfLine) && (skip & SkipSymbols.EndOfLine) != 0) ||
                 ((symbol.SymbolType == SymbolType.SingleLineComment) && (skip & SkipSymbols.SingleLineComment) != 0) ||
                 ((symbol.SymbolType == SymbolType.MultiLineComment) && (skip & SkipSymbols.MultiLineComment) != 0) ||
                 ((symbol.SymbolType == SymbolType.PreprocessorDirective) && (skip & SkipSymbols.Preprocessor) != 0) ||
                 ((symbol.SymbolType == SymbolType.XmlHeaderLine) && (skip & SkipSymbols.XmlHeader) != 0) ||
                 (symbol.SymbolType == SymbolType.SkippedSection)))
            {
                symbol = this.symbols.Peek(++index);
            }

            return symbol;
        }
Example #14
0
        /// <summary>
        /// Gets the next whitespace or comment symbol if it appears in the skip list.
        /// </summary>
        /// <param name="parentProxy">The proxy to add the new CodeUnits to.</param>
        /// <param name="types">Indicates the types of CodeUnits to advance past.</param>
        /// <returns>Returns true if a symbol was advanced past; false otherwise.</returns>
        private bool GetNextNonCodeSymbol(CodeUnitProxy parentProxy, SkipSymbols types)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(types);

            Symbol symbol = this.symbols.Peek(1);
            if (symbol == null)
            {
                return false;
            }

            // NOTE: If the list of case-statements checked here is changed, you must also make the same corresponding change in the PeekToNextCodeSymbol method.
            switch (symbol.SymbolType)
            {
                case SymbolType.WhiteSpace:
                    if ((types & SkipSymbols.WhiteSpace) == 0)
                    {
                        return false;
                    }

                    parentProxy.Children.Add(new Whitespace(this.document, symbol.Text, symbol.Location, this.symbols.Generated));
                    this.symbols.Advance();
                    return true;

                case SymbolType.EndOfLine:
                    if ((types & SkipSymbols.EndOfLine) == 0)
                    {
                        return false;
                    }

                    parentProxy.Children.Add(new EndOfLine(this.document, symbol.Text, symbol.Location, this.symbols.Generated));
                    this.symbols.Advance();
                    return true;

                case SymbolType.SingleLineComment:
                    if ((types & SkipSymbols.SingleLineComment) == 0)
                    {
                        return false;
                    }

                    parentProxy.Children.Add(new SingleLineComment(this.document, symbol.Text, symbol.Location, this.symbols.Generated));
                    this.symbols.Advance();
                    return true;

                case SymbolType.MultiLineComment:
                    if ((types & SkipSymbols.MultiLineComment) == 0)
                    {
                        return false;
                    }

                    parentProxy.Children.Add(new MultilineComment(this.document, symbol.Text, symbol.Location, this.symbols.Generated));
                    this.symbols.Advance();
                    return true;

                case SymbolType.PreprocessorDirective:
                    if ((types & SkipSymbols.Preprocessor) == 0)
                    {
                        return false;
                    }

                    this.GetPreprocessorDirective(parentProxy, symbol, this.symbols.Generated);
                    this.symbols.Advance();
                    return true;

                case SymbolType.XmlHeaderLine:
                    if ((types & SkipSymbols.XmlHeader) == 0)
                    {
                        return false;
                    }

                    parentProxy.Children.Add(new ElementHeaderLine(this.document, symbol.Text, symbol.Location, this.symbols.Generated));
                    this.symbols.Advance();
                    return true;

                case SymbolType.SkippedSection:
                    parentProxy.Children.Add(new SkippedSection(this.document, symbol.Location, this.symbols.Generated, symbol.Text));
                    this.symbols.Advance();
                    return true;

                default:
                    return false;
            }
        }
Example #15
0
        /// <summary>
        /// Advances past any whitespace and comments in the code.
        /// </summary>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="skip">Indicates the types of CodeUnits to advance past.</param>
        private void AdvanceToNextCodeSymbol(CodeUnitProxy parentProxy, SkipSymbols skip)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(skip);

            while (this.GetNextNonCodeSymbol(parentProxy, skip))
            {
            }
        }