public object Construct(BaseConstructor ctor, string tag, Node node) { object result; _block.Yield(MutableString.Create(tag, RubyEncoding.GetRubyEncoding(ctor.Encoding)), ctor.ConstructPrimitive(node), out result); return(result); }
public SourceUnitTree Parse(SourceUnit /*!*/ sourceUnit, RubyCompilerOptions /*!*/ options, ErrorSink /*!*/ errorSink) { Assert.NotNull(sourceUnit, options, errorSink); ErrorCounter counter = new ErrorCounter(errorSink); _tokenizer.ErrorSink = counter; _tokenizer.Compatibility = options.Compatibility; _lexicalScopes.Clear(); EnterScope(CreateTopScope(options.LocalNames)); using (SourceCodeReader reader = sourceUnit.GetReader()) { _sourceUnit = sourceUnit; _tokenizer.Initialize(null, reader, sourceUnit, options.InitialLocation); // Default encoding when hosted (ignore KCODE, we are reading from Unicode buffer): _tokenizer.Encoding = (reader.Encoding != null) ? RubyEncoding.GetRubyEncoding(reader.Encoding) : RubyEncoding.UTF8; _tokenizer.AllowNonAsciiIdentifiers = _tokenizer.Encoding != RubyEncoding.Binary; try { Parse(); LeaveScope(); } catch (InternalSyntaxError) { _ast = null; _lexicalScopes.Clear(); } finally { ScriptCodeParseResult props; if (counter.AnyError) { _ast = null; if (_tokenizer.UnterminatedToken) { props = ScriptCodeParseResult.IncompleteToken; } else if (_tokenizer.EndOfFileReached) { props = ScriptCodeParseResult.IncompleteStatement; } else { props = ScriptCodeParseResult.Invalid; } } else { props = ScriptCodeParseResult.Complete; } sourceUnit.CodeProperties = props; } return(_ast); } }
protected override void AfterParse() { var existingSearchPaths = LanguageOptions.GetSearchPathsOption(LanguageSetup.Options) ?? LanguageOptions.GetSearchPathsOption(RuntimeSetup.Options); if (existingSearchPaths != null) { _loadPaths.InsertRange(0, existingSearchPaths); } #if !SILVERLIGHT try { string rubylib = Environment.GetEnvironmentVariable("RUBYLIB"); if (rubylib != null) { _loadPaths.AddRange(GetPaths(rubylib)); } } catch (SecurityException) { // nop } #endif LanguageSetup.Options["SearchPaths"] = _loadPaths; if (!_disableRubyGems) { _requiredPaths.Insert(0, "gem_prelude.rb"); } LanguageSetup.Options["RequiredPaths"] = _requiredPaths; LanguageSetup.Options["DefaultEncoding"] = _defaultEncoding; LanguageSetup.Options["LocaleEncoding"] = _defaultEncoding ?? #if SILVERLIGHT RubyEncoding.UTF8; #else RubyEncoding.GetRubyEncoding(Console.InputEncoding); #endif #if DEBUG && !SILVERLIGHT // Can be set to nl-BE, ja-JP, etc string culture = Environment.GetEnvironmentVariable("IR_CULTURE"); if (culture != null) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture, false); } #endif if (ConsoleOptions.DisplayVersion && ConsoleOptions.Command == null && ConsoleOptions.FileName == null) { ConsoleOptions.PrintVersion = true; ConsoleOptions.Exit = true; } }
public override void Add(SourceUnit sourceUnit, string message, SourceSpan span, int errorCode, Severity severity) { if (severity == Severity.Warning && !ReportWarning(_context.Verbose, errorCode)) { return; } CountError(severity); string path; string codeLine; RubyEncoding encoding; int line = span.Start.Line; if (sourceUnit != null) { path = sourceUnit.Path; using (SourceCodeReader reader = sourceUnit.GetReader()) { if (line > 0) { try { reader.SeekLine(line); codeLine = reader.ReadLine(); } catch (Exception) { codeLine = null; } } else { codeLine = null; } encoding = reader.Encoding != null?RubyEncoding.GetRubyEncoding(reader.Encoding) : RubyEncoding.UTF8; } } else { path = null; codeLine = null; encoding = RubyEncoding.UTF8; } if (severity == Severity.Error || severity == Severity.FatalError) { throw new SyntaxError(message, path, line, span.Start.Column, codeLine); } else { WriteMessage( MutableString.Create(RubyContext.FormatErrorMessage(message, "warning", path, line, span.Start.Column, null), encoding) ); } }
public RubyConstructor(RubyGlobalScope /*!*/ scope, NodeProvider /*!*/ nodeProvider) : base(nodeProvider, scope) { _encoding = RubyEncoding.GetRubyEncoding(nodeProvider.Encoding); _newSite = CallSite <Func <CallSite, RubyModule, object, object, object, object> > .Create( RubyCallAction.Make(scope.Context, "new", RubyCallSignature.WithImplicitSelf(3)) ); _yamlInitializeSite = CallSite <Func <CallSite, object, object, Hash, object> > .Create( RubyCallAction.Make(scope.Context, "yaml_initialize", RubyCallSignature.WithImplicitSelf(3)) ); }
/// <summary> /// If the SCRIPT_LINES__ constant is set, we need to publish the file being loaded, /// along with the contents of the file /// </summary> private void AddScriptLines(SourceUnit file) { ConstantStorage storage; if (!_context.ObjectClass.TryResolveConstant(null, "SCRIPT_LINES__", out storage)) { return; } IDictionary scriptLines = storage.Value as IDictionary; if (scriptLines == null) { return; } lock (scriptLines) { // Read in the contents of the file RubyArray lines = new RubyArray(); SourceCodeReader reader = file.GetReader(); RubyEncoding encoding = RubyEncoding.GetRubyEncoding(reader.Encoding); using (reader) { reader.SeekLine(1); while (true) { string lineStr = reader.ReadLine(); if (lineStr == null) { break; } MutableString line = MutableString.CreateMutable(lineStr.Length + 1, encoding); line.Append(lineStr).Append('\n'); lines.Add(line); } } // Publish the contents of the file, keyed by the file name MutableString path = MutableString.Create(file.Document.FileName, _context.GetPathEncoding()); scriptLines[path] = lines; } }
/// <exception cref="Exception">On error.</exception> protected override void ParseArgument(string arg) { ContractUtils.RequiresNotNull(arg, "arg"); string mainFileFromPath = null; if (arg.StartsWith("-e", StringComparison.Ordinal)) { string command; if (arg == "-e") { command = PopNextArg(); } else { command = arg.Substring(2); } LanguageSetup.Options["MainFile"] = "-e"; if (CommonConsoleOptions.Command == null) { CommonConsoleOptions.Command = String.Empty; } else { CommonConsoleOptions.Command += "\n"; } CommonConsoleOptions.Command += command; return; } if (arg.StartsWith("-S", StringComparison.Ordinal)) { mainFileFromPath = arg == "-S" ? PopNextArg() : arg.Substring(2); } if (arg.StartsWith("-I", StringComparison.Ordinal)) { string includePaths; if (arg == "-I") { includePaths = PopNextArg(); } else { includePaths = arg.Substring(2); } _loadPaths.AddRange(GetPaths(includePaths)); return; } if (arg.StartsWith("-K", StringComparison.Ordinal)) { LanguageSetup.Options["KCode"] = arg.Length >= 3 ? RubyEncoding.GetKCodingByNameInitial(arg[2]) : null; return; } if (arg.StartsWith("-r", StringComparison.Ordinal)) { _requiredPaths.Add((arg == "-r") ? PopNextArg() : arg.Substring(2)); return; } if (arg.StartsWith("-C", StringComparison.Ordinal)) { ConsoleOptions.ChangeDirectory = arg.Substring(2); return; } if (arg.StartsWith("-0", StringComparison.Ordinal) || arg.StartsWith("-C", StringComparison.Ordinal) || arg.StartsWith("-F", StringComparison.Ordinal) || arg.StartsWith("-i", StringComparison.Ordinal) || arg.StartsWith("-T", StringComparison.Ordinal) || arg.StartsWith("-x", StringComparison.Ordinal)) { throw new InvalidOptionException(String.Format("Option `{0}' not supported", arg)); } int colon = arg.IndexOf(':'); string optionName, optionValue; if (colon >= 0) { optionName = arg.Substring(0, colon); optionValue = arg.Substring(colon + 1); } else { optionName = arg; optionValue = null; } switch (optionName) { #region Ruby options case "-a": case "-c": case "--copyright": case "-l": case "-n": case "-p": case "-s": throw new InvalidOptionException(String.Format("Option `{0}' not supported", optionName)); case "-d": LanguageSetup.Options["DebugVariable"] = true; // $DEBUG = true break; case "--version": ConsoleOptions.PrintVersion = true; ConsoleOptions.Exit = true; break; case "-v": ConsoleOptions.DisplayVersion = true; goto case "-W2"; case "-W0": LanguageSetup.Options["Verbosity"] = 0; // $VERBOSE = nil break; case "-W1": LanguageSetup.Options["Verbosity"] = 1; // $VERBOSE = false break; case "-w": case "-W2": LanguageSetup.Options["Verbosity"] = 2; // $VERBOSE = true break; #endregion #if DEBUG && !SILVERLIGHT case "-DT*": SetTraceFilter(String.Empty, false); break; case "-DT": SetTraceFilter(PopNextArg(), false); break; case "-ET*": SetTraceFilter(String.Empty, true); break; case "-ET": SetTraceFilter(PopNextArg(), true); break; case "-ER": RubyOptions.ShowRules = true; break; case "-save": LanguageSetup.Options["SavePath"] = optionValue ?? AppDomain.CurrentDomain.BaseDirectory; break; case "-load": LanguageSetup.Options["LoadFromDisk"] = ScriptingRuntimeHelpers.True; break; case "-useThreadAbortForSyncRaise": RubyOptions.UseThreadAbortForSyncRaise = true; break; case "-compileRegexps": RubyOptions.CompileRegexps = true; break; #endif case "-trace": LanguageSetup.Options["EnableTracing"] = ScriptingRuntimeHelpers.True; break; case "-profile": LanguageSetup.Options["Profile"] = ScriptingRuntimeHelpers.True; break; case "-1.8.6": case "-1.8.7": case "-1.9": case "-2.0": throw new InvalidOptionException(String.Format("Option `{0}' is no longer supported. The compatible Ruby version is 1.9.", optionName)); case "-X": switch (optionValue) { case "AutoIndent": case "TabCompletion": case "ColorfulConsole": throw new InvalidOptionException(String.Format("Option `{0}' not supported", optionName)); } goto default; default: base.ParseArgument(arg); if (ConsoleOptions.FileName != null) { if (mainFileFromPath != null) { ConsoleOptions.FileName = FindMainFileFromPath(mainFileFromPath); } if (ConsoleOptions.Command == null) { SetupOptionsForMainFile(); } else { SetupOptionsForCommand(); } LanguageSetup.Options["ArgumentEncoding"] = #if SILVERLIGHT RubyEncoding.UTF8; #else RubyEncoding.GetRubyEncoding(Console.InputEncoding); #endif } break; } }