From b9a22b3af2642dcb997807c2181093fd486213b4 Mon Sep 17 00:00:00 2001 From: Djomanix Date: Fri, 27 Jun 2014 15:58:20 -0400 Subject: [PATCH] Added sources files. Signed-off-by: Djomanix --- contrib/online_ide/sources/nit/pnacl_nit.nit | 202 ++++++++++++++++++++ .../sources/nit_mode_for_ace/lib_ace_mode/nit.js | 72 +++++++ .../lib_ace_mode/nit_highlight_rules.js | 93 +++++++++ 3 files changed, 367 insertions(+) create mode 100644 contrib/online_ide/sources/nit/pnacl_nit.nit create mode 100644 contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit.js create mode 100644 contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit_highlight_rules.js diff --git a/contrib/online_ide/sources/nit/pnacl_nit.nit b/contrib/online_ide/sources/nit/pnacl_nit.nit new file mode 100644 index 0000000..8274f44 --- /dev/null +++ b/contrib/online_ide/sources/nit/pnacl_nit.nit @@ -0,0 +1,202 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014 Johan Kayser +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A version of the naive Nit interpreter for PNaCl. +module pnacl_nit + +import naive_interpreter +import debugger +import pnacl +intrude import toolcontext +intrude import modelbuilder +intrude import standard::file + +redef interface Object + # We redefine exit to start a new thread before killing the one that called exit. + redef fun exit(exit_value: Int) + do + var dictionary = new PepperDictionary + dictionary["exit"] = exit_value + dictionary["exit_thread"] = "A new thread has been made available for Nit." + create_thread + app.post_dictionary dictionary + exit_thread exit_value + end +end + +#hack realpath. +redef class String + redef fun realpath do return self + + # file_exists looks in the 'files' HashMap. + redef fun file_exists: Bool + do + if sys.files.has_key(self) then return true + return false + end +end + +redef class Sys + # We add two HashMap for temporary file storage. + # 'lib' stores the lib files. + var lib = new HashMap[String, String] + # 'files' stores the "file(s)" you want to interpret + the lib files. + var files = new HashMap[String, String] +end + +redef class ToolContext + # We don't need 'the compute_nit_dir'. + redef fun compute_nit_dir: nullable String + do + return "/pnacl" + end +end + +# We have to redef some IFStream methods because we don't use NativeFiles anymore. +redef class IFStream + + # Looks in the 'files' HashMap. + redef init open(path: String) + do + self.path = path + var file = sys.files[path] + prepare_buffer(file.length) + _buffer.append(file) + end + + redef fun close + do + end_reached = true + end + + redef fun fill_buffer + do + _buffer.clear + end_reached = true + end + + redef fun reopen + do + _buffer_pos = 0 + end +end + +redef class ModelBuilder + # We don't use paths as the interpreter, so just return the argument. + redef fun module_absolute_path(path: String): String do return path + + # We don't use paths as the interpreter, so we don't use location or lookpaths args (see the default implementation). + redef fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath + do + var candidate: nullable String = null + var try_file = "{name}.nit" + if try_file.file_exists then + if candidate == null then + candidate = try_file + else if candidate != try_file then + # try to disambiguate conflicting modules + var abs_candidate = module_absolute_path(candidate) + var abs_try_file = module_absolute_path(try_file) + if abs_candidate != abs_try_file then + toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}") + end + end + end + if candidate == null then return null + return identify_file(candidate) + end +end + +class Pnacl_nit + super PnaclApp + + # In handle_dictionary we search for the 'operation' key in dictionaries, + # 'load' means that we are loading the Nit library, + # 'interpret' launches the interpreter code. + redef fun handle_dictionary(dictionary: PepperDictionary) + do + var d = dictionary.copy + var operation = d["operation"] + + # If operation = 'intepret' we want to interpret some Nit code, so we execute the same code as in nit.nit. + if operation == "interpret" then + var args = d["args"].to_s.split(' ') + # Create a tool context to handle options and paths + var toolcontext = new ToolContext + toolcontext.tooldescription = "Usage: nit [OPTION]... ...\nInterprets and debbugs Nit programs." + # Add an option "-o" to enable compatibilit with the tests.sh script + var opt = new OptionString("compatibility (does noting)", "-o") + toolcontext.option_context.add_option(opt) + var opt_mixins = new OptionArray("Additionals module to min-in", "-m") + toolcontext.option_context.add_option(opt_mixins) + # We do not add other options, so process them now! + toolcontext.process_options(args) + + # We need a model to collect stufs + var model = new Model + # An a model builder to parse files + var modelbuilder = new ModelBuilder(model, toolcontext.as(not null)) + + var arguments = toolcontext.option_context.rest + var progname = arguments.first + sys.files[progname] = d["content"].to_s + + # Here we load an process all modules passed on the command line + var mmodules = modelbuilder.parse([progname]) + mmodules.add_all modelbuilder.parse(opt_mixins.value) + modelbuilder.run_phases + + if toolcontext.opt_only_metamodel.value then exit(0) + + var mainmodule: nullable MModule + + # Here we launch the interpreter on the main module + if mmodules.length == 1 then + mainmodule = mmodules.first + else + mainmodule = new MModule(model, null, mmodules.first.name, mmodules.first.location) + mainmodule.set_imported_mmodules(mmodules) + end + + var self_mm = mainmodule.as(not null) + var self_args = arguments.as(not null) + + if toolcontext.opt_debugger_autorun.value then + modelbuilder.run_debugger_autorun(self_mm, self_args) + else if toolcontext.opt_debugger_mode.value then + modelbuilder.run_debugger(self_mm, self_args) + else + modelbuilder.run_naive_interpreter(self_mm, self_args) + end + # If operation = 'load', we are loading lib files, so we store them into HashMaps and send a response to JS. + else if operation == "load" then + var filename = d["filename"] + var content = d["content"] + if filename isa String and content isa String then + sys.lib[filename] = content + sys.files[filename] = content + end + var response = new PepperDictionary + response["operation"] = "load_response" + response["files_number"] = sys.lib.length.to_s + post_dictionary response + end + end +end + +redef fun app do return once new Pnacl_nit +app.initialize # Needed to correctly set up Nit control over the Pepper API. +app.run # Wait for dictionaries. diff --git a/contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit.js b/contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit.js new file mode 100644 index 0000000..fb1e3dc --- /dev/null +++ b/contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit.js @@ -0,0 +1,72 @@ +define(function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +// defines the parent mode +var TextMode = require("./text").Mode; +var Tokenizer = require("../tokenizer").Tokenizer; +var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent; +var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour; + +// defines the language specific highlighters and folding rules +var NitHighlightRules = require("./nit_highlight_rules").NitHighlightRules; +//var NitFoldMode = require("./folding/nit").NitFoldMode; + +var Mode = function() { + // set everything up + this.HighlightRules = NitHighlightRules; + this.$outdent = new MatchingBraceOutdent(); + this.$behaviour = new CstyleBehaviour(); + //this.foldingRules = new NitFoldMode(); +}; +oop.inherits(Mode, TextMode); + +(function() { + // configure comment start/end characters + this.lineCommentStart = "#"; + //this.blockComment = {start: "/*", end: "*/"}; + + // special logic for indent/outdent. + // By default ace keeps indentation of previous line + this.getNextLineIndent = function(state, line, tab) { + var indent = this.$getIndent(line); + + var tokenizedLine = this.getTokenizer().getLineTokens(line, state); + var tokens = tokenizedLine.tokens; + + if (tokens.length && tokens[tokens.length-1].type == "comment") { + return indent; + } + + if (state == "start") { + //var match = line.match(/^.*[\{\(\[]\s*$/); + var match = line.match(/(^class| class | if | else | else|do$)/); + if (match) { + indent += tab + tab; + } + } + return indent; + }; + this.checkOutdent = function(state, line, input) { + return this.$outdent.checkOutdent(line, input); + }; + this.autoOutdent = function(state, doc, row) { + this.$outdent.autoOutdent(doc, row); + }; + + // create worker for live syntax checking + this.createWorker = function(session) { + /*var worker = new WorkerClient(["ace"], "ace/mode/nit_worker", "NitWorker"); + worker.attachToDocument(session.getDocument()); + worker.on("errors", function(e) { + session.setAnnotations(e.data); + }); + return worker;*/ + return null; + }; + + this.$id = "ace/mode/nit"; +}).call(Mode.prototype); + +exports.Mode = Mode; +}); diff --git a/contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit_highlight_rules.js b/contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit_highlight_rules.js new file mode 100644 index 0000000..32d9a04 --- /dev/null +++ b/contrib/online_ide/sources/nit_mode_for_ace/lib_ace_mode/nit_highlight_rules.js @@ -0,0 +1,93 @@ +define(function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules; +var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; + +var NitHighlightRules = function() { + + var keywords = ( + "package|module|import|class|abstract|interface|universal|enum|end|fun|" + + "type|init|redef|is|do|readable|writable|var|intern|extern|protected|private|" + + "intrude|if|then|else|while|loop|for|in|and|or|not|implies|return|continue|" + + "break|abort|assert|new|isa|once|super|self|true|false|null|as|nullable|isset|label|__debug__" + ); + + var keywordMapper = this.createKeywordMapper({ + "keyword": keywords, + "variable.language": "self", + "constant.language": "null|true|false" + }, "identifier"); + + // regexp must not have capturing parentheses. Use (?:) instead. + // regexps are ordered -> the first match is used + this.$rules = { + "start" : [ + { + token : "comment", + regex : "#.*$" + }, + { + token : "support.class", + regex : /[A-Z]{1}[a-z]+/ + }, + DocCommentHighlightRules.getStartRule("doc-start"), + { + token : "string.regexp", + regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)" + }, { + token : "string", // character + regex : /'(?:.|\\(:?u[\da-fA-F]+|x[\da-fA-F]+|[tbrf'"n]))'/ + }, { + token : "string", start : '"', end : '"|$', next: [ + {token: "constant.language.escape", regex: /\\(:?u[\da-fA-F]+|x[\da-fA-F]+|[tbrf'"n])/}, + {token: "invalid", regex: /\\./} + ] + }, { + token : "string", start : '@"', end : '"', next:[ + {token: "constant.language.escape", regex: '""'} + ] + }, { + token : "constant.numeric", // hex + regex : "0[xX][0-9a-fA-F]+\\b" + }, { + token : "constant.numeric", // float + regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b" + }, { + token : "constant.language.boolean", + regex : "(?:true|false)\\b" + }, { + token : keywordMapper, + regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b" + }, { + token : "keyword.operator", + regex : "@|!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\::|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)" + }, { + token : "keyword", + regex : "^\\s*#(if|else|elif|endif|define|undef|warning|error|line|region|endregion|pragma)" + }, { + token : "punctuation.operator", + regex : "\\?|\\:|\\,|\\;|\\." + }, { + token : "paren.lparen", + regex : "[[({]" + }, { + token : "paren.rparen", + regex : "[\\])}]" + }, { + token : "text", + regex : "\\s+" + } + ] + }; + this.embedRules(DocCommentHighlightRules, "doc-", + [ DocCommentHighlightRules.getEndRule("start") ]); + this.normalizeRules(); +}; + +oop.inherits(NitHighlightRules, TextHighlightRules); + +exports.NitHighlightRules = NitHighlightRules; + +}); -- 1.7.9.5