Beispiel #1
0
        public static bool IsOperator(this SyntaxTokenType token)
        {
            switch (token)
            {
            case SyntaxTokenType.OpAdd:
            case SyntaxTokenType.OpSubtract:
            case SyntaxTokenType.OpMod:
            case SyntaxTokenType.OpDivide:
            case SyntaxTokenType.OpMultiply:
            case SyntaxTokenType.OpCompareAnd:
            case SyntaxTokenType.OpCompareOr:
            case SyntaxTokenType.OpCompareLe:
            case SyntaxTokenType.OpCompareLt:
            case SyntaxTokenType.OpCompareGe:
            case SyntaxTokenType.OpCompareGt:
            case SyntaxTokenType.OpCompareEq:
            case SyntaxTokenType.OpCompareNe:
            case SyntaxTokenType.OpAssignment:
            case SyntaxTokenType.OpDecrementAssign:
            case SyntaxTokenType.OpShiftLeft:
            case SyntaxTokenType.OpShiftRight:
                return(true);

            default:
                return(false);
            }
        }
Beispiel #2
0
        protected int GetExpression(
            SyntaxTokenType openerType,
            SyntaxTokenType closerType,
            int index,
            List <SyntaxToken> tokens)
        {
            var nestingLevel = 0;

            do
            {
                var token = tokens[index];
                if (token.Type == openerType)
                {
                    nestingLevel++;
                }
                if (token.Type == closerType)
                {
                    nestingLevel--;
                }
                index++;
            }while ((nestingLevel > 0));

            //Return one less as we will over-step by one
            return(index - 1);
        }
 public static void TypeIs(SyntaxToken token, SyntaxTokenType type, string paramName)
 {
     if (token != null)
     {
         Requires.That(token.Type == type, paramName, $"Token must be {type}");
     }
 }
Beispiel #4
0
        public static TokenParser Get(SyntaxTokenType tokenType)
        {
            if (_cachedTokenParsers.Value.TryGetValue(tokenType, out var parser))
            {
                return(parser);
            }

            return(new TokenParser(tokenType));
        }
Beispiel #5
0
        public SyntaxToken(SyntaxTokenType type, TextPosition position, string text, string valueText)
        {
            Requires.EnumDefined(type, nameof(type));
            Requires.NotNullOrEmpty(text, nameof(text));

            Type      = type;
            Position  = position;
            Text      = text;
            ValueText = valueText;
        }
        public SyntaxToken(SyntaxTokenType type, TextPosition position, string text, string valueText)
        {
            Requires.EnumDefined(type, nameof(type));
            Requires.NotNullOrEmpty(text, nameof(text));

            Type = type;
            Position = position;
            Text = text;
            ValueText = valueText;
        }
Beispiel #7
0
        private void Add(SyntaxTokenType token, string s)
        {
            if (_needToWriteIndent && _indent.Length > 0)
            {
                WriteToken(new SyntaxToken(SyntaxTokenType.Literal, _indent));
            }

            WriteToken(new SyntaxToken(token, s));
            _needToWriteIndent = false;
            _shouldWriteLine   = true;
        }
Beispiel #8
0
        protected int GetNext(
            int index, List <SyntaxToken> tokens, SyntaxTokenType tokenType)
        {
            var currentTokenType = SyntaxTokenType.Unknown;

            do
            {
                currentTokenType = tokens[index++].Type;
            }while (currentTokenType != tokenType);
            return(--index);
        }
Beispiel #9
0
        protected bool LineContains(int i, List <SyntaxToken> tokens, SyntaxTokenType tokenType)
        {
            while (++i < tokens.Count() && tokens[i].Type != SyntaxTokenType.SemiColon)
            {
                if (tokens[i].Type == tokenType)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #10
0
 public SyntaxToken(string text, int position, SyntaxTokenType tokenType)
 {
     this.Text      = text;
     this.Position  = position;
     this.TokenType = tokenType;
 }
 public static SyntaxToken LastOrDefault(this IEnumerable<SyntaxToken> source, SyntaxTokenType type) {
     return source.LastOrDefault(t => t.Type == type);
 }
Beispiel #12
0
 internal SyntaxToken(SyntaxTokenType type)
 {
     TokenType = type;
     FullWidth = Text.Length;
 }
Beispiel #13
0
        private void Add(SyntaxTokenType token, string s)
        {
            if (_needToWriteIndent && _indent.Length > 0)
                WriteToken(new SyntaxToken(SyntaxTokenType.Literal, _indent));

            WriteToken(new SyntaxToken(token, s));
            _needToWriteIndent = false;
            _shouldWriteLine = true;
        }
Beispiel #14
0
 public static string GetDefaultTokenValue(SyntaxTokenType tokenType)
 {
     return(_defaultTokenValues.Value.TryGetValue(tokenType, out var value) ? value : null);
 }
Beispiel #15
0
 public SyntaxToken(SyntaxTokenType tokenType)
 {
     TokenType  = tokenType;
     TokenValue = GetDefaultTokenValue(tokenType) ?? throw new ArgumentException($"{tokenType} has no default value.", nameof(tokenType));
 }
Beispiel #16
0
 internal SyntaxTokenWithValue(SyntaxTokenType type, T value) : this(type, value.ToString(), value)
 {
 }
Beispiel #17
0
 internal SyntaxTokenWithValue(SyntaxTokenType type, string text, T value) : base(type, text.Length)
 {
     _text  = text;
     _value = value;
 }
 public SyntaxToken PreviousToken(SyntaxTokenType type) {
     return SyntaxTree?.Tokens.NextOrPrevious(Parent, this, type, nextToken: false) ?? Missing;
 }
        internal SyntaxToken(SyntaxNode parent, SyntaxTokenType type, SyntaxTokenClassification classification, TextExtent extent) {
            _extent  = extent;
            _parent  = parent;

            _classificationAndType = ((int)type << TypeBitShift) | ((int)classification << ClassificationBitShift);
        }
 public static IEnumerable<SyntaxToken> OfType(this IEnumerable<SyntaxToken> source, SyntaxTokenType type) {
     return source.Where(t => t.Type == type);
 }
Beispiel #21
0
        private TokenParser(SyntaxTokenType tokenType)
        {
            _cachedTokenParsers.Value.Add(tokenType, this);

            _tokenType = tokenType;
        }
 public SyntaxException(SyntaxTokenType type, char character, StringBuilder redValue)
     : base($"Enexpected character '{character}' after '{redValue}' in '{type}' token.")
 {
 }
Beispiel #23
0
 public SyntaxToken(SyntaxTokenType type, string token)
 {
     Type = type;
     Token = token;
 }
Beispiel #24
0
 private static Parser <SyntaxToken, SyntaxToken> Token(SyntaxTokenType tokenType) => Parser <SyntaxToken> .Token(token => token.TokenType == tokenType);
Beispiel #25
0
 public SyntaxToken(SyntaxTokenType tokenType, string tokenValue)
 {
     TokenType  = tokenType;
     TokenValue = tokenValue;
 }
Beispiel #26
0
 private static Parser <SyntaxToken, Unit> Keyword(SyntaxTokenType keyword) => Keyword(keyword, Unit.Value);
Beispiel #27
0
 internal SyntaxIdentifier(SyntaxTokenType type, string name)
 // Text가 아직 설정되지 않은 상태에서 base 생성자에서는 Text.Length 를 사용해서
     : base(type, name.Length)
 {
     _name = name;
 }
Beispiel #28
0
 internal static SyntaxToken WithValue <T>(SyntaxTokenType type, string text, T value)
 => new SyntaxTokenWithValue <T>(type, text, value);
 public SyntaxToken(SyntaxTokenType type, string token)
 {
     Type  = type;
     Token = token;
 }
Beispiel #30
0
        public static string GetText(SyntaxTokenType type)
        {
            switch (type)
            {
            case SyntaxTokenType.PlusToken:
                return("+");

            case SyntaxTokenType.MinusToken:
                return("-");

            case SyntaxTokenType.AsteriskToken:
                return("*");

            case SyntaxTokenType.SlashToken:
                return("/");

            case SyntaxTokenType.PercentToken:
                return("%");

            case SyntaxTokenType.ConcatToken:
                return("++");

            case SyntaxTokenType.GreaterToken:
                return(">");

            case SyntaxTokenType.LessToken:
                return("<");

            case SyntaxTokenType.GreaterEqualToken:
                return(">=");

            case SyntaxTokenType.LessEqualToken:
                return("<=");

            case SyntaxTokenType.EqualToken:
                return("==");

            case SyntaxTokenType.NotEqualToken:
                return("!=");

            case SyntaxTokenType.LShiftToken:
                return("<<");

            case SyntaxTokenType.RShiftToken:
                return(">>");

            case SyntaxTokenType.ExclamationToken:
                return("!");

            case SyntaxTokenType.TildeToken:
                return("~");

            case SyntaxTokenType.VBarToken:
                return("|");

            case SyntaxTokenType.AmperToken:
                return("&");

            case SyntaxTokenType.DoubleVBarToken:
                return("||");

            case SyntaxTokenType.DoubleAmperToken:
                return("&&");

            case SyntaxTokenType.CaretToken:
                return("^");

            case SyntaxTokenType.AssignToken:
                return("=");

            case SyntaxTokenType.LParenToken:
                return("(");

            case SyntaxTokenType.RParenToken:
                return(")");

            case SyntaxTokenType.LBraceToken:
                return("{");

            case SyntaxTokenType.RBraceToken:
                return("}");

            case SyntaxTokenType.TrueKeyword:
                return("true");

            case SyntaxTokenType.FalseKeyword:
                return("false");

            case SyntaxTokenType.WildcardKeyword:
                return("_");

            case SyntaxTokenType.EOF:
                return("\0");

            case SyntaxTokenType.ImportKeyword:
                return("import");

            case SyntaxTokenType.BreakKeyword:
                return("break");

            case SyntaxTokenType.ContinueKeyword:
                return("continue");

            case SyntaxTokenType.ReturnKeyword:
                return("return");

            case SyntaxTokenType.ModuleKeyword:
                return("module");

            case SyntaxTokenType.StructKeyword:
                return("struct");

            case SyntaxTokenType.FnKeyword:
                return("fn");

            case SyntaxTokenType.VarKeyword:
                return("var");

            case SyntaxTokenType.IfKeyword:
                return("if");

            case SyntaxTokenType.WhileKeyword:
                return("while");

            case SyntaxTokenType.ForKeyword:
                return("for");

            default:
                return(string.Empty);
            }
        }
 private SyntaxToken(SyntaxTokenType type, string value)
 {
     Type = type;
     Value = value;
 }
Beispiel #32
0
 protected SyntaxToken(SyntaxTokenType type, int width)
 {
     TokenType = type;
     FullWidth = width;
 }
Beispiel #33
0
 private static Parser <SyntaxToken, T> Keyword <T>(SyntaxTokenType keyword, T result) => Token(keyword).ThenReturn(result).Labelled($"'{keyword}' keyword");
 public static SyntaxToken FirstOrMissing(this IEnumerable<SyntaxToken> source, SyntaxTokenType type) {
     return source.Where(t => t.Type == type).DefaultIfEmpty(SyntaxToken.Missing).First();
 }
Beispiel #35
0
 private static Parser <SyntaxToken, T> Operator <T>(SyntaxTokenType @operator, T result) => Token(@operator).ThenReturn(result).Labelled($"'{@operator}' operator");
Beispiel #36
0
 public static SyntaxToken KeywordToken(SyntaxTokenType type)
 => _tokens[type - FirstToken];