本文整理汇总了C#中TokenStream.Next方法的典型用法代码示例。如果您正苦于以下问题:C# TokenStream.Next方法的具体用法?C# TokenStream.Next怎么用?C# TokenStream.Next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TokenStream
的用法示例。
在下文中一共展示了TokenStream.Next方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseIfStatement
private static Ast.If ParseIfStatement(
TokenStream Stream,
ParseContext Context)
{
if (Stream.AtEnd() || Stream.Next().Type != TokenType.Identifier || Stream.Next().Value.ToUpper() != "IF")
throw new CompileError("[009] Impossible error: If parse entered, no if found.", Stream);
var r = new Ast.If(Stream.Next());
Stream.Advance(); //Skip 'if'
r.Header = ParseExpression(Stream, Context, TokenType.OpenBrace);
if (Stream.Next().Type != TokenType.OpenBrace) throw new CompileError("[00A] Expected {", Stream);
r.ThenBlock = ParseBlock(Stream, Context);
if (Stream.Next().Type == TokenType.Identifier && Stream.Next().Value.ToUpper() == "ELSE")
{
Stream.Advance();
if (Stream.Next().Type == TokenType.OpenBrace)
r.ElseBlock = ParseBlock(Stream, Context);
else if (Stream.Next().Type == TokenType.Identifier && Stream.Next().Value.ToUpper() == "IF")
r.ElseBlock = ParseIfStatement(Stream, Context);
}
return r;
}
示例2: ParseStaticInvokation
public static List<Ast.Node> ParseStaticInvokation(
TokenStream Stream,
ParseContext Context)
{
if (Stream.Next().Type != TokenType.OpenBracket) throw new CompileError("[017] Expected [", Stream);
Stream.Advance();
var parameters = new List<Ast.Node>();
while (true)
{
if (IsEndOfStatement(Stream)) throw new CompileError("[018] Expected ]", Stream);
if (Stream.Next().Type == TokenType.CloseBracket)
{
Stream.Advance();
return parameters;
}
parameters.Add(ParseTerm(Stream, Context));
}
}
示例3: ParseExpression
private static Ast.Node ParseExpression(
TokenStream Stream,
ParseContext Context,
Predicate<TokenStream> IsTerminal)
{
if (Stream.Next().Type == TokenType.Identifier && Stream.Next().Value.ToUpper() == "LAMBDA")
{
var declaration = ParseMacroDeclaration(Stream, Context);
declaration.Type = DeclarationType.Lambda;
return new Ast.Lambda(declaration.Body.Body.Source, declaration, "LAMBDA");
}
else if (Stream.Next().Type == TokenType.Identifier && Stream.Next().Value.ToUpper() == "NEW")
return ParseNew(Stream, Context);
else
return ParseExpression(ParseTerm(Stream, Context), Stream, Context, 0, IsTerminal);
}
示例4: ParseStatement
private static Ast.Statement ParseStatement(
TokenStream Stream,
ParseContext Context)
{
Ast.Statement r = null;
var firstToken = Stream.Next().Value.ToUpper();
if (firstToken == "LET")
{
r = ParseLetStatement(Stream, Context);
}
else if (firstToken == "IF")
{
r = ParseIfStatement(Stream, Context);
}
else if (firstToken == "RETURN")
{
r = ParseReturnStatement(Stream, Context);
}
else if (firstToken == "VAR" || firstToken == "VARIABLE")
{
r = ParseLocalDeclaration(Stream, Context);
}
else
{
//If it's not any special statement, it must be a function call.
var parameters = ParseStaticInvokationStatement(Stream, Context);
var control = Context.FindControl(parameters);
if (control != null)
{
Ast.Node childBlock = null;
if (control.BlockType == ControlBlockType.RequiredBlock)
{
if (Stream.Next().Type == TokenType.OpenBrace)
childBlock = ParseBlock(Stream, Context);
else
throw new CompileError("[00C] Expected {", Stream);
}
else
{
if (Stream.Next().Type != TokenType.Semicolon) throw new CompileError("[00D] Expected ;", Stream);
Stream.Advance();
}
r = new Ast.ControlInvokation(parameters[0].Source, control, parameters, childBlock);
}
else
{
r = new Ast.StaticInvokation(parameters[0].Source, parameters);
if (Stream.Next().Type != TokenType.Semicolon) throw new CompileError("[00E] Expected ;", Stream);
Stream.Advance();
}
}
return r;
}
示例5: Style
public void Style(TokenStream Stream)
{
this.Stream = Stream;
this.FoldLevel = 1;
while (!Stream.AtEnd())
{
if (Stream.Next().Value.ToUpper() == "MACRO"
|| Stream.Next().Value.ToUpper() == "FUNCTION"
|| Stream.Next().Value.ToUpper() == "TEST")
ParseMacroDeclaration();
else if (Stream.Next().Value.ToUpper() == "RULE")
ParseRuleDeclaration();
else if (Stream.Next().Value.ToUpper() == "TYPE")
ParseTypeDeclaration();
else if (Stream.Next().Value.ToUpper() == "GLOBAL")
ParseGlobalDeclaration();
else if (Stream.Next().Value.ToUpper() == "INCLUDE")
{
Style(TokenStyle.Keyword);
Expect(TokenType.String);
}
else if (Stream.Next().Value.ToUpper() == "DEFAULT")
{
Style(TokenStyle.Keyword);
Expect(TokenType.Identifier, t => t.Value.ToUpper() == "OF");
ParseRuleDeclaration();
}
else
Stream.Advance();
}
}
示例6: ParseLocalDeclaration
private static Ast.LocalDeclaration ParseLocalDeclaration(
TokenStream Stream,
ParseContext Context)
{
var start = Stream.Next();
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[003] Expected identifier", Stream);
var r = new Ast.LocalDeclaration(start);
r.Name = Stream.Next().Value;
//r.Typename = "GENERIC";
Stream.Advance();
if (Stream.Next().Type == TokenType.Colon)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[004] Expected identifier", Stream);
r.Typename = Stream.Next().Value.ToUpper();
Stream.Advance();
}
if (Stream.Next().Type == TokenType.Operator && Stream.Next().Value == "=")
{
Stream.Advance();
r.Value = ParseExpression(Stream, Context, TokenType.Semicolon);
}
if (!IsEndOfStatement(Stream)) throw new CompileError("[005] Expected ;", Stream);
Stream.Advance();
return r;
}
示例7: ParseOptionalDot
private static Ast.Node ParseOptionalDot(
TokenStream Stream,
Ast.Node LHS,
ParseContext Context)
{
if (Stream.AtEnd()) return LHS;
if (Stream.Next().Type == TokenType.Dot)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[00F] Dot operator must be followed by identifier", Stream);
var RHS = Stream.Next().Value;
var MA = new Ast.MemberAccess(LHS.Source);
MA.Object = LHS;
MA.Name = RHS.ToUpper();
Stream.Advance();
return ParseOptionalDot(Stream, MA, Context);
}
else if (Stream.Next().Type == TokenType.At)
{
Stream.Advance();
var RHS = ParseTerm(Stream, Context);
var I = new Ast.Indexer(LHS.Source);
I.Object = LHS;
I.Index = RHS;
return ParseOptionalDot(Stream, I, Context);
}
else if (Stream.Next().Type == TokenType.Colon)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[013] Expected identifier", Stream);
var C = new Ast.Cast(Stream.Next(), LHS, Stream.Next().Value.ToUpper());
Stream.Advance();
return ParseOptionalDot(Stream, C, Context);
}
else
return LHS;
}
示例8: ParseMacroDeclarationHeader
internal static List<DeclarationTerm> ParseMacroDeclarationHeader(TokenStream Stream,
DeclarationHeaderTerminatorType TerminatorType)
{
var r = new List<DeclarationTerm>();
while (true)
{
if (TerminatorType == DeclarationHeaderTerminatorType.StreamEnd && Stream.AtEnd()) return r;
else if (Stream.Next().Type == TokenType.Colon) return r;
else if (TerminatorType == DeclarationHeaderTerminatorType.OpenBrace && Stream.Next().Type == TokenType.OpenBrace) return r;
else if (TerminatorType == DeclarationHeaderTerminatorType.OpenBraceOrWhen)
{
if (Stream.Next().Type == TokenType.OpenBrace) return r;
else if (Stream.Next().Value.ToUpper() == "WHEN") return r;
else if (Stream.Next().Value.ToUpper() == "WITH") return r;
else if (Stream.Next().Value.ToUpper() == "ORDER") return r;
}
r.Add(ParseDeclarationTerm(Stream));
}
}
示例9: ParseMemberDeclaration
internal static Variable ParseMemberDeclaration(TokenStream Stream, ParseContext Context)
{
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[032] Expected identifier", Stream.Next());
Stream.Advance();
var r = new Variable();
r.StorageMethod = VariableStorageMethod.Member;
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[033] Expected identifier", Stream.Next());
r.Name = Stream.Next().Value.ToUpper();
Stream.Advance();
if (Stream.Next().Type == TokenType.Colon)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[034] Expected identifier", Stream.Next());
r.DeclaredTypeName = Stream.Next().Value.ToUpper();
Stream.Advance();
}
if (!Stream.AtEnd() && Stream.Next().Type == TokenType.QuestionMark)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.String) throw new CompileError("Expected documentation", Stream);
r.Documentation = Stream.Next().Value;
Stream.Advance();
}
if (Stream.Next().Type != TokenType.Semicolon) throw new CompileError("[035] Expected ;", Stream.Next());
Stream.Advance();
return r;
}
示例10: ParseGlobalDeclaration
internal static Variable ParseGlobalDeclaration(TokenStream Stream, ParseContext Context)
{
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[032] Expected identifier", Stream.Next());
Stream.Advance();
var r = new Variable();
r.StorageMethod = VariableStorageMethod.Member;
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[033] Expected identifier", Stream.Next());
var start = Stream.Next();
r.Name = Stream.Next().Value.ToUpper();
Stream.Advance();
if (Stream.Next().Type == TokenType.Colon)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[034] Expected identifier", Stream.Next());
r.DeclaredTypeName = Stream.Next().Value.ToUpper();
Stream.Advance();
}
if (Stream.Next().Type == TokenType.Operator && Stream.Next().Value == "=")
{
Stream.Advance();
var initialValue = ParseExpression(Stream, Context, TokenType.Semicolon);
var initializer = new Ast.Let(start, new Ast.Identifier(start), initialValue);
Context.Initialization.Add(initializer);
}
if (!Stream.AtEnd() && Stream.Next().Type == TokenType.QuestionMark)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.String) throw new CompileError("Expected documentation", Stream);
r.Documentation = Stream.Next().Value;
Stream.Advance();
}
if (Stream.Next().Type != TokenType.Semicolon) throw new CompileError("[035] Expected ;", Stream.Next());
Stream.Advance();
return r;
}
示例11: ParseMacroDeclaration
internal static Declaration ParseMacroDeclaration(TokenStream Stream, ParseContext Context)
{
if (Stream.AtEnd())
throw new CompileError("[028] Impossible error: ParseDeclaration entered at end of stream.", Stream);
try
{
var r = new Declaration();
r.ReturnTypeName = "VOID";
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[029] Expected identifier", Stream.Next());
r.Type = DeclarationType.Macro;
Stream.Advance();
r.Terms = ParseMacroDeclarationHeader(Stream, DeclarationHeaderTerminatorType.OpenBrace);
foreach (var t in r.Terms)
if (t.Type == DeclarationTermType.Operator)
r.DefinesOperator = true;
if (r.DefinesOperator)
{
if (r.Terms.Count != 3 ||
r.Terms[0].Type != DeclarationTermType.Term ||
r.Terms[1].Type != DeclarationTermType.Operator ||
r.Terms[2].Type != DeclarationTermType.Term)
throw new CompileError("Operator macros must be of the form 'term op term'", Stream);
r.Terms[1].Type = DeclarationTermType.Keyword;
}
if (!Stream.AtEnd() && Stream.Next().Type == TokenType.Colon)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[02A] Expected identifier", Stream);
r.ReturnTypeName = Stream.Next().Value.ToUpper();
Stream.Advance();
}
if (r.DefinesOperator && r.ReturnTypeName == "VOID")
throw new CompileError("Operator macros must return a value.", Stream);
if (!Stream.AtEnd() && Stream.Next().Type == TokenType.OpenBrace)
{
r.Body = new LambdaBlock(ParseBlock(Stream, Context));
}
else
throw new CompileError("[02B] Expected block", Stream);
if (!Stream.AtEnd() && Stream.Next().Type == TokenType.QuestionMark)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.String) throw new CompileError("Expected documentation", Stream);
r.Documentation = Stream.Next().Value;
Stream.Advance();
}
return r;
}
catch (CompileError ce)
{
throw ce;
}
catch (Exception e)
{
throw new CompileError(e.Message + e.StackTrace, Stream);
}
}
示例12: Build
public static List<Declaration> Build(
TokenStream Stream,
ParseContext Context,
Func<String,ErrorStrategy> OnError,
bool PrepareInitializer = true)
{
var r = new List<Declaration>();
while (!Stream.AtEnd())
{
try
{
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[036] Expected identifier", Stream);
if (Stream.Next().Value.ToUpper() == "MACRO" || Stream.Next().Value.ToUpper() == "FUNCTION")
{
var declaration = ParseMacroDeclaration(Stream, Context);
declaration.OwnerContextID = Context.ID;
Context.PendingEmission.Add(declaration);
}
else if (Stream.Next().Value.ToUpper() == "TEST")
{
var declaration = ParseMacroDeclaration(Stream, Context);
declaration.Type = DeclarationType.Test;
declaration.OwnerContextID = Context.ID;
r.Add(declaration);
Context.PendingEmission.Add(declaration);
}
else if (Stream.Next().Value.ToUpper() == "RULE")
{
var declaration = ParseRuleDeclaration(Stream, Context);
declaration.OwnerContextID = Context.ID;
var rulebook = Context.Rules.FindMatchingRulebook(declaration.Terms);
if (rulebook == null)
{
rulebook = new Rulebook { DeclarationTerms = new List<DeclarationTerm>(
declaration.Terms.Select(t => t.GenericClone())) };
rulebook.ResultTypeName = declaration.ReturnTypeName;
Context.Rules.Rulebooks.Add(rulebook);
}
//if (Declaration.AreTermTypesCompatible(rulebook.DeclarationTerms, declaration.Terms) == false)
// throw new CompileError("[037] Term types are not compatible with existing rulebook", Stream);
if (declaration.ReturnTypeName.ToUpper() != rulebook.ResultTypeName.ToUpper())
throw new CompileError("Rule return type not compatible with existing rulebook", Stream);
rulebook.Rules.Add(declaration);
Context.PendingEmission.Add(declaration);
}
else if (Stream.Next().Value.ToUpper() == "TYPE")
{
var type = ParseTypeDeclaration(Stream, Context);
if (Context.ActiveScope.FindType(type.Name) != null) throw new CompileError("[038] Type already defined", Stream);
Context.ActiveScope.Types.Add(type);
}
else if (Stream.Next().Value.ToUpper() == "GLOBAL")
{
var variable = ParseGlobalDeclaration(Stream, Context);
variable.StorageMethod = VariableStorageMethod.Static;
Context.ActiveScope.Variables.Add(variable);
}
else if (Stream.Next().Value.ToUpper() == "INCLUDE")
{
Stream.Advance();
if (Stream.Next().Type != TokenType.String) throw new CompileError("Expected string", Stream);
var filename = Stream.Next().Value;
Stream.Advance();
if (Context.FileLoader == null) throw new CompileError("Inclusion not enabled", Stream);
var file = Context.FileLoader.LoadFile(filename, Stream.CurrentFile);
var newStream = new TokenStream(new StringIterator(file.Data), Context);
newStream.CurrentFile = file;
r.AddRange(Build(newStream, Context, OnError, false));
}
else if (Stream.Next().Value.ToUpper() == "DEFAULT")
{
Stream.Advance();
if (Stream.Next().Value.ToUpper() != "OF") throw new CompileError("Expected 'OF'", Stream);
Stream.Advance();
if (Stream.Next().Value.ToUpper() != "RULE") throw new CompileError("Expected 'RULE'", Stream);
var declaration = ParseRuleDeclaration(Stream, Context);
foreach (var term in declaration.Terms)
if (!String.IsNullOrEmpty(term.DeclaredTypeName) && term.DeclaredTypeName != "GENERIC")
throw new CompileError("Don't declare types for default rules.");
declaration.OwnerContextID = Context.ID;
var rulebook = Context.Rules.FindMatchingRulebook(declaration.Terms);
if (rulebook == null)
{
rulebook = new Rulebook { DeclarationTerms = declaration.Terms };
rulebook.ResultTypeName = declaration.ReturnTypeName;
Context.Rules.Rulebooks.Add(rulebook);
}
//if (Declaration.AreTermTypesCompatible(rulebook.DeclarationTerms, declaration.Terms) == false)
// throw new CompileError("[037] Term types are not compatible with existing rulebook", Stream);
if (declaration.ReturnTypeName.ToUpper() != rulebook.ResultTypeName.ToUpper())
throw new CompileError("Rule return type not compatible with existing rulebook", Stream);
if (rulebook.DefaultValue != null)
throw new CompileError("Rulebook already has a default value", Stream);
rulebook.DefaultValue = declaration;
Context.PendingEmission.Add(declaration);
}
else
throw new CompileError("[039] Unknown declaration type", Stream);
}
catch (Exception e)
//.........这里部分代码省略.........
示例13: ParseDeclarationTerm
internal static DeclarationTerm ParseDeclarationTerm(TokenStream Stream)
{
DeclarationTerm r = null;
var start = Stream.Next();
if (Stream.Next().Type == TokenType.Identifier)
{
r = new DeclarationTerm
{
Name = Stream.Next().Value.ToUpper(),
Type = DeclarationTermType.Keyword,
RepetitionType = DeclarationTermRepetitionType.Once
};
Stream.Advance();
}
else if (Stream.Next().Type == TokenType.Operator)
{
r = new DeclarationTerm
{
Name = Stream.Next().Value.ToUpper(),
Type = DeclarationTermType.Operator,
RepetitionType = DeclarationTermRepetitionType.Once
};
Stream.Advance();
}
else if (Stream.Next().Type == TokenType.OpenParen)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[021] Expected identifier", start);
r = new DeclarationTerm
{
Name = Stream.Next().Value.ToUpper(),
Type = DeclarationTermType.Term,
RepetitionType = DeclarationTermRepetitionType.Once
};
Stream.Advance();
if (Stream.Next().Type == TokenType.Colon)
{
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier)
throw new CompileError("[022] Expected identifier", start);
var declaredType = Stream.Next().Value;
r.DeclaredTypeName = declaredType.ToUpper();
Stream.Advance();
}
if (Stream.Next().Type != TokenType.CloseParen)
throw new CompileError("[023] Expected )", start);
Stream.Advance();
}
else
throw new CompileError("[025] Illegal token in declaration header", start);
if (!Stream.AtEnd())
{
if (Stream.Next().Type == TokenType.QuestionMark)
{
var marker = Stream.Next();
var repetitionMarker = Stream.Next().Value;
Stream.Advance();
if (repetitionMarker == "?")
{
if (r.Type != DeclarationTermType.Keyword)
throw new CompileError("[026] Only keywords can be optional in a declaration header", Stream);
r.RepetitionType = DeclarationTermRepetitionType.Optional;
}
//Left over from when terms could be repeated.
//else if (repetitionMarker == "+")
// r.RepetitionType = DeclarationTermRepetitionType.OneOrMany;
//else if (repetitionMarker == "*")
// r.RepetitionType = DeclarationTermRepetitionType.NoneOrMany;
else
throw new CompileError("[027] Unrecognized repetition marker on declaration term", marker);
}
}
return r;
}
示例14: ParseStaticInvokationStatement
public static List<Ast.Node> ParseStaticInvokationStatement(
TokenStream Stream,
ParseContext Context)
{
var parameters = new List<Ast.Node>();
while (true)
{
if (IsEndOfStatement(Stream) || Stream.Next().Type == TokenType.OpenBrace)
{
//Why don't we skip the semi-colon? This is called when parsing control macros where it might be optional.
return parameters;
}
parameters.Add(ParseTerm(Stream, Context));
}
}
示例15: ParseInitializers
private static List<Ast.Initializer> ParseInitializers(
TokenStream Stream,
ParseContext Context)
{
Stream.Advance();
var r = new List<Ast.Initializer>();
while (Stream.Next().Type != TokenType.CloseBrace)
{
var initializerStart = Stream.Next();
if (Stream.Next().Type != TokenType.Identifier || Stream.Next().Value.ToUpper() != "LET")
throw new CompileError("[01E] Expected let", Stream);
Stream.Advance();
if (Stream.Next().Type != TokenType.Identifier) throw new CompileError("[01E] Expected identifier", Stream);
var memberName = Stream.Next().Value.ToUpper();
Stream.Advance();
if (Stream.Next().Type != TokenType.Operator && Stream.Next().Value != "=")
throw new CompileError("[01F] Expected =", Stream);
Stream.Advance();
var value = ParseExpression(Stream, Context, TokenType.Semicolon);
if (Stream.Next().Type != TokenType.Semicolon) throw new CompileError("[020] Expected ;", Stream);
Stream.Advance();
r.Add(new Ast.Initializer(initializerStart, memberName, value));
}
Stream.Advance();
return r;
}