private static int ParseInteger(RubyContext /*!*/ context, string /*!*/ str) { bool isNegative = false; if (str[0] == '-') { isNegative = true; str = str.Remove(0, 1); } Tokenizer tokenizer = new Tokenizer(false, ErrorSink.Null); tokenizer.Initialize(context.CreateSnippet(str, SourceCodeKind.File)); tokenizer.GetNextToken(); TokenValue value = tokenizer.TokenValue; TokenValueType type = value.Type; tokenizer.GetNextToken(); TokenValueType nextType = tokenizer.TokenValue.Type; // We are only interested in the whole string being a valid Integer if (type == TokenValueType.Integer && nextType == TokenValueType.None) { return(isNegative ? -value.Integer : value.Integer); } else { throw RubyExceptions.CreateTypeConversionError("String", "Integer"); } }
public AssertTokenizer /*!*/ Load(object /*!*/ source) // source: byte[] or string { _tests.Assert(_log.Errors.Count == 0, "Previous test case reported unexpected error/warning(s)"); SourceUnit sourceUnit; RubyEncoding encoding; byte[] binarySource = source as byte[]; if (binarySource != null) { encoding = RubyEncoding.Binary; sourceUnit = _context.CreateSourceUnit(new BinaryContentProvider(binarySource), null, encoding.Encoding, SourceCodeKind.File); } else { encoding = DefaultEncoding; sourceUnit = _context.CreateSnippet((string)source, SourceCodeKind.File); } _tokenizer = new Tokenizer(false, DummyVariableResolver.AllMethodNames) { ErrorSink = _log, Compatibility = Compatibility, Encoding = encoding, Verbatim = Verbatim, }; _tokenizer.Initialize(sourceUnit); _allTokens = new List <Tokens>(); _allValues = new List <object>(); return(this); }
public static List<Tokens> GetRubyTokens(RubyContext context, ErrorSink log, string source, bool dumpTokens, bool dumpReductions) { Parser parser = new Parser(); List<Tokens> tokens = new List<Tokens>(); if (dumpTokens) { parser.Tokenizer.EnableLogging(1, Console.Out); } parser.TokenSink = delegate(Tokens token, SourceSpan span) { tokens.Add(token); }; #if DEBUG if (dumpReductions) { DefaultParserLogger.Attach(parser, Console.Out); } #endif parser.Parse(context.CreateSnippet(source, SourceCodeKind.File), new RubyCompilerOptions(), log); //tokenizer.Initialize(SourceUnit.CreateSnippet(RB, source)); //List<Tokens> tokens = new List<Tokens>(); //Tokens token; //while ((token = tokenizer.GetNextToken()) != Tokens.EOF) { // tokens.Add(token); //} return tokens; }
public AssertTokenizer /*!*/ Load(string /*!*/ source) { Tests.Assert(_log.Errors.Count == 0, "Previous test case reported unexpected error/warning(s)"); _tokenizer = new Tokenizer(false, _log); _tokenizer.Compatibility = _context.RubyOptions.Compatibility; _tokenizer.Initialize(_context.CreateSnippet(source, SourceCodeKind.File)); _allTokens = new List <Tokens>(); _allValues = new List <object>(); return(this); }
// This method uses the tokenizer to auto-detect the base type -- happens when agument to to_i is 0 private static object ParseInteger(RubyContext/*!*/ context, string/*!*/ str) { bool isNegative = false; if (str.Length == 0) { return ScriptingRuntimeHelpers.Int32ToObject(0); } str = ParseSign(str, ref isNegative); Tokenizer tokenizer = new Tokenizer(false, ErrorSink.Null); tokenizer.Initialize(context.CreateSnippet(str, SourceCodeKind.File)); tokenizer.GetNextToken(); TokenValue value = tokenizer.TokenValue; TokenValueType type = value.Type; if (type == TokenValueType.Integer) return ScriptingRuntimeHelpers.Int32ToObject(isNegative ? -value.Integer: value.Integer); else if (type == TokenValueType.BigInteger) return isNegative ? BigInteger.Negate(value.BigInteger) : value.BigInteger; else return ScriptingRuntimeHelpers.Int32ToObject(0); }
private static int ParseInteger(RubyContext/*!*/ context, string/*!*/ str) { bool isNegative = false; if (str[0] == '-') { isNegative = true; str = str.Remove(0, 1); } Tokenizer tokenizer = new Tokenizer(false, ErrorSink.Null); tokenizer.Initialize(context.CreateSnippet(str, SourceCodeKind.File)); tokenizer.GetNextToken(); TokenValue value = tokenizer.TokenValue; TokenValueType type = value.Type; tokenizer.GetNextToken(); TokenValueType nextType = tokenizer.TokenValue.Type; // We are only interested in the whole string being a valid Integer if (type == TokenValueType.Integer && nextType == TokenValueType.None) { return isNegative ? -value.Integer : value.Integer; } else { throw RubyExceptions.CreateTypeConversionError("String", "Integer"); } }
public static object Evaluate(MutableString /*!*/ code, RubyScope /*!*/ targetScope, object self, RubyModule module, MutableString file, int line) { Assert.NotNull(code, targetScope); RubyContext context = targetScope.RubyContext; RubyMethodScope methodScope = targetScope.GetInnerMostMethodScope(); Utils.Log(Interlocked.Increment(ref _stringEvalCounter).ToString(), "EVAL"); // we want to create a new top-level local scope: var options = CreateCompilerOptionsForEval(targetScope, methodScope, module != null, line); SourceUnit source = context.CreateSnippet(code.ConvertToString(), file != null ? file.ConvertToString() : "(eval)", SourceCodeKind.Statements); Expression <EvalEntryPointDelegate> lambda; try { lambda = context.ParseSourceCode <EvalEntryPointDelegate>(source, options, context.RuntimeErrorSink); } catch (SyntaxError e) { Utils.Log(e.Message, "EVAL_ERROR"); Utils.Log(new String('-', 50), "EVAL_ERROR"); Utils.Log(source.GetCode(), "EVAL_ERROR"); Utils.Log(new String('-', 50), "EVAL_ERROR"); throw; } Debug.Assert(lambda != null); Proc blockParameter; RubyMethodInfo methodDefinition; if (methodScope != null) { blockParameter = methodScope.BlockParameter; methodDefinition = methodScope.Method; } else { blockParameter = null; methodDefinition = null; } if (context.Options.InterpretedMode) { return(Interpreter.TopLevelExecute(new InterpretedScriptCode(lambda, source), targetScope, self, module, blockParameter, methodDefinition, targetScope.RuntimeFlowControl )); } else { return(lambda.Compile(source.EmitDebugSymbols)( targetScope, self, module, blockParameter, methodDefinition, targetScope.RuntimeFlowControl )); } }