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); } }
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}"); } }
public static TokenParser Get(SyntaxTokenType tokenType) { if (_cachedTokenParsers.Value.TryGetValue(tokenType, out var parser)) { return(parser); } return(new TokenParser(tokenType)); }
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; }
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; }
protected int GetNext( int index, List <SyntaxToken> tokens, SyntaxTokenType tokenType) { var currentTokenType = SyntaxTokenType.Unknown; do { currentTokenType = tokens[index++].Type; }while (currentTokenType != tokenType); return(--index); }
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); }
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); }
internal SyntaxToken(SyntaxTokenType type) { TokenType = type; FullWidth = Text.Length; }
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; }
public static string GetDefaultTokenValue(SyntaxTokenType tokenType) { return(_defaultTokenValues.Value.TryGetValue(tokenType, out var value) ? value : null); }
public SyntaxToken(SyntaxTokenType tokenType) { TokenType = tokenType; TokenValue = GetDefaultTokenValue(tokenType) ?? throw new ArgumentException($"{tokenType} has no default value.", nameof(tokenType)); }
internal SyntaxTokenWithValue(SyntaxTokenType type, T value) : this(type, value.ToString(), value) { }
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); }
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.") { }
public SyntaxToken(SyntaxTokenType type, string token) { Type = type; Token = token; }
private static Parser <SyntaxToken, SyntaxToken> Token(SyntaxTokenType tokenType) => Parser <SyntaxToken> .Token(token => token.TokenType == tokenType);
public SyntaxToken(SyntaxTokenType tokenType, string tokenValue) { TokenType = tokenType; TokenValue = tokenValue; }
private static Parser <SyntaxToken, Unit> Keyword(SyntaxTokenType keyword) => Keyword(keyword, Unit.Value);
internal SyntaxIdentifier(SyntaxTokenType type, string name) // Text가 아직 설정되지 않은 상태에서 base 생성자에서는 Text.Length 를 사용해서 : base(type, name.Length) { _name = name; }
internal static SyntaxToken WithValue <T>(SyntaxTokenType type, string text, T value) => new SyntaxTokenWithValue <T>(type, text, value);
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; }
protected SyntaxToken(SyntaxTokenType type, int width) { TokenType = type; FullWidth = width; }
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(); }
private static Parser <SyntaxToken, T> Operator <T>(SyntaxTokenType @operator, T result) => Token(@operator).ThenReturn(result).Labelled($"'{@operator}' operator");
public static SyntaxToken KeywordToken(SyntaxTokenType type) => _tokens[type - FirstToken];