public static _ATHToken Read(string source, ref int position, _ATHLexer lexer, Colour expressionColour)
 {
     try
     {
         var expression = lexer.ReadExpression(source, ref position, expressionColour, typeof(SemicolonToken));
         return(new ExpressionOrCommandToken(expression));
     }
     catch
     {
         StringBuilder commandBuilder = new StringBuilder();
         int           bracketDepth   = 0;
         while (bracketDepth != 0 || source[position] != ')')
         {
             if (source[position] == '(')
             {
                 bracketDepth++;
             }
             else if (source[position] == ')')
             {
                 bracketDepth--;
             }
             commandBuilder.Append(source[position]);
             position++;
         }
         return(new ExpressionOrCommandToken(commandBuilder.ToString()));
     }
 }
Exemple #2
0
        public static _ATHToken Read(string source, ref int position, _ATHLexer lexer, Colour expressionColour)
        {
            var expressions = new List <_ATHExpression>();

            while (source.Length > position && source[position] != '}')
            {
                while (_ATHLexer.MaybeSkipColour(source, ref position, expressionColour))
                {
                    _ATHLexer.SkipWhitespace(source, ref position);
                }
                expressions.Add(lexer.ReadExpression(source, ref position, expressionColour));
                _ATHLexer.SkipWhitespace(source, ref position);
            }

            if (position >= source.Length)
            {
                throw new TokenException("} expected at " + position + ".");
            }

            position++;
            return(new CurlyCloseDelimitedExpressionListToken(expressions.ToArray()));
        }
        public static _ATHToken Read(string source, ref int position, _ATHLexer lexer, Colour expressionColour)
        {
            bool finished            = false;
            var  expressions         = new List <Tuple <Colour, _ATHExpression> >();
            var  lineBuffer          = new Dictionary <Colour, string>();
            var  lineBufferPositions = new Dictionary <Colour, int>();

            while (finished == false)
            {
                ProcessLine(source, ref position, out finished, lexer, expressionColour, ref expressions, ref lineBuffer, ref lineBufferPositions);
            }

            foreach (var colour in lineBuffer.Keys)
            {
                if (lineBufferPositions[colour] != lineBuffer[colour].Length)
                {
                    throw new _ATHParserException("Unfinished expression in colour #" + colour.HexString.ToUpperInvariant() + ".");
                }
            }

            return(new ColouredExpressionsEndingWithTHISListDIEToken(expressions.ToArray()));
        }
        private static void ProcessLine(string source, ref int position, out bool finished, _ATHLexer lexer, Colour expressionColour, ref List <Tuple <Colour, _ATHExpression> > expressions, ref Dictionary <Colour, string> lineBuffer, ref Dictionary <Colour, int> lineBufferPositions)
        {
            Colour lineColour = expressionColour;

            _ATHLexer.SkipWhitespace(source, ref position);
            if (source[position] == '#')
            {
                lineColour = ((ColourToken)ColourToken.Read(source, ref position)).Colour;
            }

            if (lineBuffer.ContainsKey(lineColour) == false)
            {
                lineBuffer[lineColour]          = "";
                lineBufferPositions[lineColour] = 0;
            }

            lineBuffer[lineColour] += ((EverythingExceptNewlineToken)EverythingExceptNewlineToken.Read(source, ref position)).Content + "\n";

            while (position < source.Length && (source[position] == '\r' || source[position] == '\n'))
            {
                position++;
            }

            try
            {
                var pos = lineBufferPositions[lineColour];
                _ATHLexer.SkipWhitespace(lineBuffer[lineColour], ref pos);
                var expression = lexer.ReadExpression(lineBuffer[lineColour], ref pos, lineColour);
                expressions.Add(Tuple.Create(lineColour, expression));
                _ATHLexer.SkipWhitespace(lineBuffer[lineColour], ref pos);
                lineBufferPositions[lineColour] = pos;
            }
            catch (Exception)
            {
                // Empty.
            }

            if (expressions.Count > 0)
            {
                if (expressions[expressions.Count - 1].Item1 == expressionColour)
                {
                    if (expressions[expressions.Count - 1].Item2 is THISListDIEExpression)
                    {
                        finished = true;
                        return;
                    }
                }
            }

            finished = false;
        }
Exemple #5
0
        public static _ATHToken Read(string source, ref int position, _ATHLexer lexer, Colour expressionColour)
        {
            var expression = lexer.ReadExpression(source, ref position, expressionColour, typeof(SemicolonToken));

            return(new ExpressionToken(expression));
        }