Beispiel #1
0
            public double Eval(Expression es)
            {

                switch (Kind)
                {
                    case TokenKind.Number: return Num;
                    case TokenKind.Identifier: return es.GetVar(Ident);
                    case TokenKind.Add: return Tok1.Eval(es) + Tok2.Eval(es);
                    case TokenKind.Mul: return Tok1.Eval(es) * Tok2.Eval(es);
                    case TokenKind.Sub: return (Tok2==null) ? -Tok1.Eval(es) : (Tok1.Eval(es) - Tok2.Eval(es));
                    case TokenKind.Div: return Tok1.Eval(es) / Tok2.Eval(es);
                    case TokenKind.Eq: return (Math.Abs(Tok1.Eval(es)-Tok2.Eval(es)) < 1e-6) ? 1:0;
                    case TokenKind.Neq: return (Math.Abs(Tok1.Eval(es)-Tok2.Eval(es)) < 1e-6) ? 0:1;
                    case TokenKind.Leq: return (Tok1.Eval(es) <= Tok2.Eval(es)) ? 1:0;
                    case TokenKind.Geq:  return(Tok1.Eval(es) >= Tok2.Eval(es)) ? 1:0;
                    case TokenKind.Le: return (Tok1.Eval(es) < Tok2.Eval(es)) ? 1:0;
                    case TokenKind.Ge:  return(Tok1.Eval(es) > Tok2.Eval(es)) ? 1:0;
                    case TokenKind.Mod:  return(Tok1.Eval(es) % Tok2.Eval(es));
                    case TokenKind.If: return (Math.Abs(Tok1.Eval(es))>1e-6) ? Tok2.Eval(es):Tok3.Eval(es);
                    case TokenKind.Shl: return (int)Tok1.Eval(es)<<(int)Tok2.Eval(es);
                    case TokenKind.Shr: return (int)Tok1.Eval(es)>>(int)Tok2.Eval(es);
                    case TokenKind.And: return (Math.Abs(Tok1.Eval(es))>1e-6 && Math.Abs(Tok2.Eval(es))>1e-6) ?1:0;
                    case TokenKind.Or: return (Math.Abs(Tok1.Eval(es))>1e-6 || Math.Abs(Tok2.Eval(es))>1e-6) ?1:0;
                    case TokenKind.BitAnd: return (int)Tok1.Eval(es)&(int)Tok2.Eval(es);
                    case TokenKind.BitOr: return (int)Tok1.Eval(es)|(int)Tok2.Eval(es);
                    case TokenKind.BitXor: return (int)Tok1.Eval(es)^(int)Tok2.Eval(es);
                    default: throw new ExpressionException("Unknown operator: " + Kind.ToString());
                }
            }
        public static string GetText(this TokenKind kind)
        {
            // ToDo: SourceCodeGenerators would be better for this

            if (!kind.HasDefaultText())
            {
#if DEBUG
                var attr = typeof(TokenKind).GetMember(kind.ToString())
                           .FirstOrDefault(m => m.DeclaringType == typeof(TokenKind))
                           .GetCustomAttribute <DescriptionAttribute>();

                Debug.Assert(attr is null, $"TokenKind.{kind} should have HasDefaultText == true");
#endif
                return(null);
            }

            var attribute = typeof(TokenKind).GetMember(kind.ToString())
                            .FirstOrDefault(m => m.DeclaringType == typeof(TokenKind))
                            .GetCustomAttribute <DescriptionAttribute>();

            if (attribute is null)
            {
                throw new Exception(
                          $"TokenKind.{kind} should have a Description attribute with the default text");
            }

            return(attribute.Description);
        }
Beispiel #3
0
        private static string GetTokenKindName(TokenKind kind)
        {
            var type       = kind.GetType();
            var memberInfo = type.GetMember(kind.ToString());

            if (memberInfo.Length <= 0)
            {
                return(kind.ToString());
            }
            var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            return(attrs.Length > 0
                ? ((DescriptionAttribute)attrs[0]).Description
                : kind.ToString());
        }
        public static bool HasDefaultText(this TokenKind kind)
        {
            switch (kind)
            {
            case TokenKind.EoF:
            case TokenKind.Plus:
            case TokenKind.Minus:
            case TokenKind.Star:
            case TokenKind.ForwardSlash:
            case TokenKind.Bang:
            case TokenKind.BangEquals:
            case TokenKind.Ampersand:
            case TokenKind.AmpersandAmperand:
            case TokenKind.Equals:
            case TokenKind.EqualsEquals:
            case TokenKind.Pipe:
            case TokenKind.PipePipe:
            case TokenKind.OpenParenthesis:
            case TokenKind.CloseParenthesis:
            case TokenKind.OpenBrace:
            case TokenKind.CloseBrace:
            case TokenKind.Comma:
            case TokenKind.Colon:
            case TokenKind.SemiColon:
            case TokenKind.Less:
            case TokenKind.LessOrEquals:
            case TokenKind.Greater:
            case TokenKind.GreaterOrEquals:
            case TokenKind.Tilde:
            case TokenKind.Hat:
                return(true);

            case TokenKind.BreakKeyword:
            case TokenKind.ContinueKeyword:
            case TokenKind.ElseKeyword:
            case TokenKind.IfKeyword:
            case TokenKind.FalseKeyword:
            case TokenKind.ForKeyword:
            case TokenKind.FunctionKeyword:
            case TokenKind.LetKeyword:
            case TokenKind.ReturnKeyword:
            case TokenKind.ToKeyword:
            case TokenKind.TrueKeyword:
            case TokenKind.VarKeyword:
            case TokenKind.WhileKeyword:
                return(true);

            default:
#if DEBUG
                // Extra check on the codebase
                var attr = typeof(TokenKind).GetMember(kind.ToString())
                           .FirstOrDefault(m => m.DeclaringType == typeof(TokenKind))
                           .GetCustomAttribute <DescriptionAttribute>();

                Debug.Assert(attr is null, $"TokenKind.{kind} should have HasDefaultText == true");
#endif

                return(false);
            }
        }
Beispiel #5
0
        private Token Match(TokenKind kind)
        {
            if (_lookahead.Kind == kind)
                return ReadToken();

            throw new ParserError(_lookahead.Position, "Expected '" + kind.ToString() + "', found '" + _lookahead.ToString() + "'");
        }
Beispiel #6
0
 public override void PrintOp(int depth)
 {
     base.PrintOp(depth);
     //Console.WriteLine(this.ToString());
     LeftExpression.PrintOp(depth + 1);
     Console.WriteLine("\t" + Op.ToString());
     RightExpression.PrintOp(depth + 1);
 }
Beispiel #7
0
        internal static string ToString(TokenKind spec)
        {
#if SALTARELLE
            return(Enum.ToString(typeof(TokenKind), spec));
#else
            return(spec.ToString());
#endif
        }
Beispiel #8
0
        private Token Match(TokenKind kind)
        {
            if (_lookahead.Kind == kind)
            {
                return(ReadToken());
            }

            throw new ParserError(_lookahead.Position, "Expected '" + kind.ToString() + "', found '" + _lookahead.ToString() + "'");
        }
        //reporting the error
        void ReportError(Token received, TokenKind expected)
        {
            if (received.Kind.ToString() != expected.ToString())
            {
                System.Console.WriteLine("The token We have is :   " + "[" + received + "]" + "The token we expected is   :  " + "[" + expected + "]");
            }


            errorCount++;
        }
Beispiel #10
0
 private void Accept(TokenKind expected)
 {
     if (currentToken.Kind == expected)
     {
         AcceptIt();
     }
     else
     {
         SyntaxError(expected.ToString() + " expected, but "+currentToken.Kind+" was found");
     }
 }
Beispiel #11
0
        public override void PrintOp(int depth)
        {
            base.PrintOp(depth);
            for (var i = 0; i <= depth; i++)
            {
                Console.Write('\t');
            }

            Console.WriteLine(Op.ToString());
            Expression.PrintOp(depth + 1);
        }
 public override string ToString()
 {
     if (Kind == TokenKind.Number)
     {
         return(Value.ToString());
     }
     else
     {
         return(Kind.ToString());
     }
 }
Beispiel #13
0
        public static string GetDescription(this TokenKind kind)
        {
            var type       = typeof(TokenKind);
            var memInfo    = type.GetMember(kind.ToString());
            var attributes = memInfo[0].GetCustomAttributes(typeof(TokenDescriptionAttribute), false);

            if (attributes.Length > 0)
            {
                return(((TokenDescriptionAttribute)attributes[0]).Description);
            }
            return(String.Empty);
        }
        // Проверить, что тип текущего распознанного токена совпадает с заданным.
        // Если совпадает, то распознать следующий токен, иначе синтаксическая ошибка.
        private Token Match(TokenKind tkn)
        {
            if (lexAn.Token.Type == tkn) // Сравниваем.
            {
                var token = lexAn.Token;
                lexAn.RecognizeNextToken(); // Распознаем следующий токен.
                return(token);
            }
            else
            {
                SyntaxError("Ожидалось " + tkn.ToString()); // Обнаружена синтаксическая ошибка.
            }

            return(null);
        }
Beispiel #15
0
        static public string TokenKindToString(TokenKind s)
        {
            switch (s)
            {
            case TokenKind.ADD:
                return("+");

            case TokenKind.SUB:
                return("-");

            case TokenKind.LPAR:
                return("(");

            case TokenKind.RPAR:
                return(")");

            case TokenKind.LBRA:
                return("{");

            case TokenKind.RBRA:
                return("}");

            case TokenKind.LT:
                return("<");

            case TokenKind.GT:
                return(">");

            case TokenKind.EQU:
                return("=");

            case TokenKind.SEMI:
                return(";");

            case TokenKind.END:
                return("#");

            default:
                return(s.ToString());
            }
        }
Beispiel #16
0
        protected void Expect(TokenKind kind, string text = null)
        {
            bool emitError = true;

            while (enumerator_.TryPeek(out var next))
            {
                if (kind == next.Kind && (text == null || text.Equals(next.Text, StringComparison.OrdinalIgnoreCase)))
                {
                    enumerator_.MoveNext();
                    return;
                }

                if (emitError)
                {
                    if (text != null && kind == next.Kind)
                    {
                        EmitError(string.Format(Errors.UnexpectedTokenText, text, next.Text));
                    }
                    else
                    {
                        EmitError(string.Format(Errors.UnexpectedTokenKind,
                                                text != null ? $"{kind}('{text}')" : kind.ToString(),
                                                $"{next.Kind}('{next.Text}')"));
                    }

                    emitError = false;
                }

                if (CanRecoverInParentScope(next.Kind, next.Text, out var scope))
                {
                    throw new ErrorRecoveryException(scope);
                }

                enumerator_.MoveNext();
            }

            throw new ParsingException(Errors.UnexpectedEndOfFile, CurrentToken.Span);
        }
Beispiel #17
0
 /// <summary>
 /// 各種トークンを受け取りStringBuilderに格納します
 /// </summary>
 public void Write(TokenKind kind, string value)
 {
     switch (kind)
     {
         case TokenKind.None:
             builder.Append(Escape(value));
             break;
         case TokenKind.WhiteSpace:
         case TokenKind.EOL:
             builder.Append(Escape(value));
             break;
         default:
             builder.Append("<span class=\"" + kind.ToString() + "\">" + Escape(value) + "</span>");
             break;
     }
 }
Beispiel #18
0
 public override string ToString()
 {
     return(kind.ToString());
 }
Beispiel #19
0
 private string BuildFilename(TokenKind kind)
 {
     return(kind.ToString() + "_" + AccessTokenFn);
 }
Beispiel #20
0
        private OperatorKind TokenKindToOperator(Position position, TokenKind kind)
        {
            switch (kind)
            {
            case TokenKind.Asterisk: return(OperatorKind.Multiplication);

            case TokenKind.Backslash: return(OperatorKind.Difference);

            case TokenKind.Equal: return(OperatorKind.Equality);

            case TokenKind.GreaterThan: return(OperatorKind.GreaterThan);

            case TokenKind.LessThan: return(OperatorKind.LessThan);

            case TokenKind.Minus: return(OperatorKind.Subtraction);

            case TokenKind.Plus: return(OperatorKind.Addition);

            default: throw new ParserError(position, "Invalid operator encountered: " + kind.ToString());
            }
        }
Beispiel #21
0
        private string TokenKindToOperatorName(Position position, TokenKind kind)
        {
            switch (kind)
            {
            case TokenKind.Asterisk: return("*");

            case TokenKind.Backslash: return("\\");

            case TokenKind.Equal: return("=");

            case TokenKind.GreaterThan: return(">");

            case TokenKind.LessThan: return("<");

            case TokenKind.Minus: return("-");

            case TokenKind.Plus: return("+");

            default: throw new ParserError(position, "Invalid operator encountered: " + kind.ToString());
            }
        }
Beispiel #22
0
 public override string ToString()
 {
     return(tk.ToString());
 }
Beispiel #23
0
 public void UsesDescriptiveNameForToString()
 {
     lower.ToString().ShouldBe("Lowercase");
     upper.ToString().ShouldBe("Uppercase");
     caseInsensitive.ToString().ShouldBe("Case Insensitive");
 }
Beispiel #24
0
 private string TokenKindToOperatorName(Position position, TokenKind kind)
 {
     switch (kind)
     {
         case TokenKind.Asterisk   : return "*";
         case TokenKind.Backslash  : return "\\";
         case TokenKind.Equal      : return "=";
         case TokenKind.GreaterThan: return ">";
         case TokenKind.LessThan   : return "<";
         case TokenKind.Minus      : return "-";
         case TokenKind.Plus       : return "+";
         default: throw new ParserError(position, "Invalid operator encountered: " + kind.ToString());
     }
 }
Beispiel #25
0
 public override string ToString()
 {
     return(string.Format("{0}: {1} {2}", Position.ToString(), Kind.ToString(), Text));
 }
        private SyntaxToken EatToken(TokenKind tokenKind)
        {
            SyntaxToken syntax;

            if (_token.Kind == tokenKind)
            {
                syntax = Open <SyntaxToken>();
            }
            else
            {
                // Create an invalid token in case we don't match it
                var invalid = Open <InvalidSyntaxToken>();
                invalid.InvalidKind = _token.Kind;
                syntax = invalid;
                var tokenText          = tokenKind.ToText();
                var expectingTokenText = tokenText != null ? $"while expecting `{tokenText}` (token: `{tokenKind.ToString().ToLowerInvariant()}`)" : $"while expecting token `{tokenKind.ToString().ToLowerInvariant()}`";
                if (_token.Kind == TokenKind.Invalid)
                {
                    LogError($"Unexpected token found `{ToPrintable(_token)}` {expectingTokenText}");
                }
                else
                {
                    LogError($"Unexpected token found `{ToPrintable(_token)}` (token: `{_token.Kind.ToString().ToLowerInvariant()}`) {expectingTokenText}");
                }
            }
            syntax.TokenKind = tokenKind;
            syntax.Text      = _token.Kind.ToText() ?? _token.GetText(_lexer.Source);
            if (tokenKind == TokenKind.NewLine)
            {
                // Once we have found a new line, we let all the other NewLines as trivias
                _hideNewLine = true;
            }
            NextToken();
            return(Close(syntax));
        }
Beispiel #27
0
 private Token MatchCurrent(TokenKind l)
 {
     if (l != _enumerator.Current.Kind)
     {
         throw new SyntaxException("Got " + _enumerator.Current.Kind.ToString() + $", {l.ToString()} expected" +
                                   $" at {_enumerator.Current.row + 1}:{_enumerator.Current.column}",
                                   _enumerator.Current.row, _enumerator.Current.column);
     }
     else
     {
         //Console.WriteLine(_enumerator.Current.ToString());
         return(_enumerator.Current);
     }
 }
Beispiel #28
0
 private string GetDebuggerDisplay()
 {
     return((Text == null) ?
            Kind.ToString() :
            $"{Kind}: \"{Text}\"");
 }
Beispiel #29
0
 private OperatorKind TokenKindToOperator(Position position, TokenKind kind)
 {
     switch (kind)
     {
         case TokenKind.Asterisk: return OperatorKind.Multiplication;
         case TokenKind.Backslash: return OperatorKind.Difference;
         case TokenKind.Equal: return OperatorKind.Equality;
         case TokenKind.GreaterThan: return OperatorKind.GreaterThan;
         case TokenKind.LessThan: return OperatorKind.LessThan;
         case TokenKind.Minus: return OperatorKind.Subtraction;
         case TokenKind.Plus: return OperatorKind.Addition;
         default: throw new ParserError(position, "Invalid operator encountered: " + kind.ToString());
     }
 }
Beispiel #30
0
 private ExpressionType GetBinaryOperator(TokenKind kind)
 {
     return(kind switch
     {
         TokenKind.Add => ExpressionType.Add,
         TokenKind.Subtract => ExpressionType.Subtract,
         TokenKind.Multiply => ExpressionType.Multiply,
         TokenKind.Divide => ExpressionType.Divide,
         TokenKind.Mod => ExpressionType.Modulo,
         TokenKind.Power => ExpressionType.Power,
         TokenKind.LessThan => ExpressionType.LessThan,
         TokenKind.LessThanOrEqual => ExpressionType.LessThanOrEqual,
         TokenKind.Equal => ExpressionType.Equal,
         TokenKind.GreaterThanOrEqual => ExpressionType.GreaterThanOrEqual,
         TokenKind.GreaterThan => ExpressionType.GreaterThan,
         TokenKind.NotEqual => ExpressionType.NotEqual,
         _ => throw new CompilerException(tokenizer.Position, string.Format("operator TokenKind:{0} error!", kind.ToString())),
     });
Beispiel #31
0
 /// <summary>
 /// Reports an error and then throws a {@link SyntaxError} exception. It calls
 /// {@link #RaiseSyntacticError(string, string)} using the spelling of the
 /// given token kind, or the empty string if the token kind is null.
 /// </summary>
 /// <param name="messageTemplate">
 /// the message template
 /// </param>
 /// <param name="kind">
 /// the token kind
 /// </param>
 /// <throws type="SyntaxError">
 /// a syntactic error
 /// </throws>
 void RaiseSyntacticError(string messageTemplate, TokenKind kind)
 {
     RaiseSyntacticError(messageTemplate, kind.ToString());
 }
 private SyntaxError NotRecognizedToken(TokenKind token)
 {
     return(NotRecognizedToken(token.ToString()));
 }
Beispiel #33
0
 private SyntaxException UnexpectedToken(TokenKind expected)
 {
     return(UnexpectedToken(expected.ToString()));
 }
Beispiel #34
0
        private void ValidateExpression(StringTokenizer t, TokenKind endToken)
        {
            this.Transforms = new List <Transform>();
            this.Errors     = new List <TokenError>();

            while (!t.EOF)
            {
                Token token = t.Next();

                if (token.Kind != TokenKind.Word)
                {
                    this.Errors.Add(new TokenError("A transform name was expected", token.Line, token.Column, token.Value, this.context));
                    return;
                }

                if (!ActiveConfig.XmlConfig.Transforms.Contains(token.Value))
                {
                    this.Errors.Add(new TokenError("The transform was not found", token.Line, token.Column, token.Value, this.context));
                }
                else
                {
                    this.Transforms.Add(ActiveConfig.XmlConfig.Transforms[token.Value]);
                }

                token = t.Next();

                if (token.Kind == TokenKind.EOF)
                {
                    if (endToken == TokenKind.EOF)
                    {
                        return;
                    }
                    else
                    {
                        this.Errors.Add(new TokenError(string.Format("Expected '>>' or '{0}'", endToken.ToString()), token.Line, token.Column, token.Value, this.context));
                    }
                }
                else if (token.Kind == endToken)
                {
                    return;
                }
                else if (token.Kind == TokenKind.TransformOperator)
                {
                    continue;
                }
                else
                {
                    this.Errors.Add(new TokenError(string.Format("Expected '>>' or '{0}'", endToken.ToString()), token.Line, token.Column, token.Value, this.context));
                    return;
                }
            }
        }