public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { while ((++sourceCode).SpecialChar) { } if (sourceCode.Peek() != '{') { sourceCode.Throw(String.Format("Error parsing a 'do' statement, expected a '{' but got '{0}' instead.", sourceCode.Peek())); } List<List<List<Token>>> code = engine.BuildLongTokens(ref sourceCode, ref script, new[] {'}'}); if (!sourceCode.SeekToNext("while")) { sourceCode.Throw("Error parsing a 'do' statement, was expecting a 'while' after the { } block."); } if (!sourceCode.SeekToNext('(')) { sourceCode.Throw("Error parsing a 'do' statement, was expecting a '(' after 'while'."); } List<List<Token>> exitCondition = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {')'}); return new DoWhileToken(code, exitCondition); }
public override bool AdvancedBuild(ScriptEngine engine, ref SourceCode sourceCode, ref Script script) { _returnElements = new Dictionary<string, List<object>>(); string id = ""; // Seek past the original text... sourceCode += _pattern.Length; if (sourceCode.Peek() == '"') { // We are being given an "id" as well. sourceCode.SeekToNext('"'); while ((++sourceCode).CurrentCode != '"') { id += sourceCode.CurrentCode; } } if (!sourceCode.SeekToNext('{')) { throw new Exception("Excpected {"); } while ((++sourceCode).Peek() != '}') { while (sourceCode.SpecialChar) { sourceCode++; } // Find a match. int index = sourceCode.Offset; for (int i = 0; i < _structure._elements.Count; i++) { if (_structure._elements[i].ConditionMatch(sourceCode)) { _structure._elements[i].Build(ref sourceCode, ref script, engine, this); break; } } if (index == sourceCode.Offset) { // We haven't progressed. throw new Exception(String.Format("Unexpected string {0}...", sourceCode.CurrentCode)); } } if (_builtEvent == null) { // Should throw an exception or something? } else { _builtEvent(id, _returnElements); } return true; }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { sourceCode += 5; if (!sourceCode.SeekToNext('(')) { sourceCode.Throw(String.Format("Expected '(', got '{0}' instead", sourceCode.NextCode)); } List<Token> tokens = engine.BuildTokens(ref sourceCode, ref script, new[] {')'}); sourceCode.SeekToNext(';'); // Go to the ; return new FixedToken(tokens); }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { // while (condition) { /* Code */ } sourceCode += 4; // The +1 comes from below. while ((++sourceCode).SpecialChar) { } if (sourceCode.CurrentCode != '(') { sourceCode.Throw("Error parsing a 'while' statement, was epexting a '(' after 'while'."); } List<List<Token>> exitCondition = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {')'}); if (!sourceCode.SeekToNext('{')) { sourceCode.Throw(String.Format("Unexpected char: '{0}'", sourceCode.CurrentCode)); } List<List<List<Token>>> code = engine.BuildLongTokens(ref sourceCode, ref script, new[] {'}'}); return new WhileLoopToken(exitCondition, code); }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { // using (arg1, arg2, arg3) { } sourceCode += 5; if (!sourceCode.SeekToNext('(')) { sourceCode.Throw("Expected ("); } // Build up the arguments. Dictionary<string, List<Token>> items = new Dictionary<string, List<Token>>(); while (sourceCode.CurrentCode != ')') { string name = sourceCode.NextWord(); if (sourceCode.CurrentCode != '=') { sourceCode.Throw(String.Format("Unexpected character, {0}.", sourceCode.CurrentCode)); } // Otherwise... List<Token> code = engine.BuildTokens(ref sourceCode, ref script, new char[] {',', ')'}); items.Add(name, code); } if (!sourceCode.SeekToNext('{')) { sourceCode.Throw(String.Format("Unexpected character, {0}", sourceCode.NextCode)); } List<List<List<Token>>> codeBlock = engine.BuildLongTokens(ref sourceCode, ref script, new char[] {'}'}); return new UsingToken(items, codeBlock); }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { if (sourceCode.Matches("//")) { sourceCode += 2; sourceCode.NextLine(); } else { sourceCode += 2; sourceCode.SeekToNext("*/"); sourceCode += 2; } return new Token.DelayToken(); }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { // if (condition) { ... } [else [if (condition) { ... }] { ... }] var ifStatement = new Statement(); sourceCode.SeekToNext('('); ifStatement.Condition = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {')'}); sourceCode++; sourceCode.SeekToNext('{'); ifStatement.Code = engine.BuildLongTokens(ref sourceCode, ref script, new[] {'}'}); sourceCode++; var elseIfStatements = new List<Statement>(); Statement elseStatement = null; while ((++sourceCode).Peek() == 'e') { bool isElseIf = false; sourceCode.SeekToNext('e'); sourceCode += 4; var s = new Statement(); if (sourceCode.Peek() == 'i') { isElseIf = true; // Get the condition; sourceCode.SeekToNext('i'); sourceCode++; sourceCode.SeekToNext('('); s.Condition = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {')'}); sourceCode++; } sourceCode.SeekToNext('{'); s.Code = engine.BuildLongTokens(ref sourceCode, ref script, new[] {'}'}); sourceCode++; if (isElseIf) { elseIfStatements.Add(s); } else { elseStatement = s; } } return new IfToken(ifStatement, elseIfStatements, elseStatement); }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { // for (init; condition; increment) { /* Code */ } sourceCode += 3; // Skip past the "for". while (char.IsWhiteSpace(sourceCode.CurrentCode)) { sourceCode++; // Offset until we reach a non-whitespace character. } if (sourceCode.CurrentCode != '(') { throw new Exception("Error parsing a 'for' statement, was expecting a '(' after 'for'"); } // Otherwise, moving along! List<List<Token>> forInit = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {';'}); sourceCode++; // We can then build the exitCondition List<List<Token>> endCondition = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {';'}); sourceCode++; // And then the increment condition. List<List<Token>> incrementStatement = engine.BuildTokensAdvanced(ref sourceCode, ref script, new[] {';', ')'}); if (!sourceCode.SeekToNext('{')) { throw new Exception("Unexpected char: " + sourceCode.CurrentCode); } List<List<List<Token>>> code = engine.BuildLongTokens(ref sourceCode, ref script, new[] {'}'}); sourceCode++; return new ForLoopToken(forInit, endCondition, incrementStatement, code); }
public override Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode) { sourceCode += 8; // Skip past function // Pattern // function functionName(args) { code } string functionName = ""; var argumentNames = new List<string>(); // Build the name first. while (sourceCode.SpecialChar) { sourceCode++; } bool space = false; while (sourceCode.CurrentCode != '(') { if (sourceCode.CurrentCode == ' ') { space = true; } else { if (space) { sourceCode.Throw("Unexpected Whitespace"); } functionName += sourceCode.CurrentCode; } sourceCode++; } // Build up arguments now. sourceCode++; space = false; string arg = ""; while (sourceCode.CurrentCode != ')') { char c = sourceCode.CurrentCode; if (c == ',') { if (string.IsNullOrEmpty(arg)) { sourceCode.Throw("Expected an argument."); } else { argumentNames.Add(arg); space = false; arg = ""; } } else { if (char.IsWhiteSpace(c) && !string.IsNullOrEmpty(arg)) { space = true; } else { if (space) { sourceCode.Throw("Unexpected whitespace"); } else if (char.IsWhiteSpace(c)) { // Do nothing... } else { arg += sourceCode.CurrentCode; } } } sourceCode++; } if (string.IsNullOrEmpty(arg)) { sourceCode.Throw("Expected an argument."); } argumentNames.Add(arg); bool result = sourceCode.SeekToNext('{'); List<List<List<Token>>> code = engine.BuildLongTokens(ref sourceCode, ref script, new[] {'}'}); return new FunctionToken(functionName, argumentNames, code); }
public override void Build(ref SourceCode code, ref Script script, ScriptEngine engine, ConfigBuilder config) { string id = ""; code += _identifier.Length; if (code.Peek() == '{' && _requiresID) { throw new Exception("Script block requires an ID."); } if (code.Peek() != '{') { code.SeekToNext('"'); while (!(++code).EOF && code.CurrentCode != '"') { id += code.CurrentCode; } if (code.EOF) { code.Throw("Unexpected EOF"); } } code.SeekToNext('{'); var c = new Code(engine.BuildLongTokens(ref code, ref script, new[] {'}'}), new Script(engine) {UseEngineGlobals = false}); config.AddReturnElement(_identifier, new ScriptBlock {Code = c, Identifier = id}); }
public override void Build(ref SourceCode code, ref Script script, ScriptEngine engine, ConfigBuilder config) { var items = new Dictionary<string, List<Token>>(); code += _identifier.Length; if (code.Peek() != '{') { throw new Exception("Syntax Error: Expected a {"); } code.SeekToNext('{'); while (!(++code).EOF && code.Peek() != '}') { // We are expecting: identifier = (code); while (code.SpecialChar) { code++; } string id = ""; bool space = false; while (!code.EOF && code.CurrentCode != '=') { if (code.CurrentCode == ' ') { space = true; } else { if (space) { code--; // Rewind, so the error is where the space is. code.Throw("Unexpected whitespace..."); } id += code.CurrentCode; } code++; } if (code.EOF) { code.Throw("Unexpected EOF"); } // Otherwise code++; List<Token> tokens = engine.BuildTokens(ref code, ref script, new[] {';'}); items.Add(id, tokens); } code.SeekToNext('}'); config.AddReturnElement(_identifier, items); }