From 14639ccc69a7fe34ce275d3578b7149763edf5c4 Mon Sep 17 00:00:00 2001 From: VapidWorx <daniel@vapidworx.com> Date: Tue, 23 Jul 2013 02:40:46 +1200 Subject: [PATCH] [powershell mode] Initial commit --- mode/powershell/index.html | 107 +++++++++++++++++++ mode/powershell/powershell.js | 191 ++++++++++++++++++++++++++++++++++ 2 files changed, 298 insertions(+) create mode 100644 mode/powershell/index.html create mode 100644 mode/powershell/powershell.js diff --git a/mode/powershell/index.html b/mode/powershell/index.html new file mode 100644 index 000000000..940840c3b --- /dev/null +++ b/mode/powershell/index.html @@ -0,0 +1,107 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>CodeMirror: Powershell mode</title> + <link rel="stylesheet" href="../../lib/codemirror.css"> + <script src="../../lib/codemirror.js"></script> + <script src="../../addon/edit/matchbrackets.js"></script> + <script src="../../addon/comment/comment.js"></script> + <script src="powershell.js"></script> + <link rel="stylesheet" href="../../doc/docs.css"> + <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style> + </head> + <body> + <h1>CodeMirror: Powershell mode</h1> + + <div><textarea id="code" name="code"> +# Literals +1234 +0.0e101 +.123 +0b01010011100 +0o01234567 +0x0987654321abcdef +7 +2147483647 +3L +79228162514264337593543950336L +0x100000000L +79228162514264337593543950336 +0xdeadbeef +3.14j +10.j +10j +.001j +1e100j +3.14e-10j + + +# String Literals +'For\'' +"God\"" +"""so loved +the world""" +'' +@" Multi +line +string "@ + +# Identifiers +$Variable = value +${Variable with spaces in it} = value + +# Operators ++ - * / % & | ^ ~ < > +== != <= >= <> << >> // ** +and or not in is + +# Delimiters +() [] {} , : ` = ; @ . # Note that @ and . require the proper context. ++= -= *= /= %= &= |= ^= +//= >>= <<= **= + +# Keywords +as assert break class continue def del elif else except +finally for from global if import lambda pass raise +return try while with yield + +# Types +bool classmethod complex dict enumerate float frozenset int list object +property reversed set slice staticmethod str super tuple type + +# Some Example code +import os +from package import ParentClass + +@nonsenseDecorator +def doesNothing(): + pass + +class ExampleClass(ParentClass): + @staticmethod + def example(inputStr): + a = list(inputStr) + a.reverse() + return ''.join(a) + + def __init__(self, mixin = 'Hello'): + self.mixin = mixin + +</textarea></div> + <script> + var editor = CodeMirror.fromTextArea(document.getElementById("code"), { + mode: {name: "powershell", + version: 2, + singleLineStringErrors: false}, + lineNumbers: true, + indentUnit: 4, + tabMode: "shift", + matchBrackets: true + }); + </script> + + + <p><strong>MIME types defined:</strong> <code>text/x-powershell</code>.</p> + </body> +</html> diff --git a/mode/powershell/powershell.js b/mode/powershell/powershell.js new file mode 100644 index 000000000..944088fbb --- /dev/null +++ b/mode/powershell/powershell.js @@ -0,0 +1,191 @@ +CodeMirror.defineMode("powershell", function() { + var ERRORCLASS = 'error'; + + function wordRegexp(words) { + return new RegExp("^((" + words.join(")|(") + "))\\b"); + } + + var wordOperators = wordRegexp(['-eq', '-ne', '-gt', '-lt', '-le', '-ge']); + var commonkeywords = ['begin', 'break', 'continue', 'do', 'default', 'else', 'elseif', + 'end', 'filter', 'for', 'foreach', 'function', 'if', 'in', 'param', + 'process', 'return', 'switch', 'until', 'where', 'while']; + + var isOperatorChar = /[+\-*&^%:=<>!|\/]/; + var isString = /("|')(\`?.)*?\1/; + + var keywords = wordRegexp(commonkeywords); + //var builtins = wordRegexp(commonBuiltins); + + var indentInfo = null; + + // tokenizers + function tokenBase(stream, state) { + + + + // Handle Comments + //var ch = stream.peek(); + + if (stream.match(keywords)) { + return('variable-2'); + } + + if (stream.match(isString)) { + return('string'); + } + + if (stream.match(wordOperators)) { + return('variable-2'); + } + if (stream.match(isOperatorChar)) { + return('variable-1'); + } + + + // Handle Variables + + + // Handle Number Literals + if (stream.match(/^[0-9\.]/, false)) { + var floatLiteral = false; + // Floats + if (stream.match(/^\d*\.\d+(e[\+\-]?\d+)?/i)) { floatLiteral = true; } + if (stream.match(/^\d+\.\d*/)) { floatLiteral = true; } + if (stream.match(/^\.\d+/)) { floatLiteral = true; } + if (floatLiteral) { + // Float literals may be "imaginary" + stream.eat(/J/i); + return 'number'; + } + // Integers + var intLiteral = false; + // Hex + if (stream.match(/^0x[0-9a-f]+/i)) { intLiteral = true; } + // Binary + if (stream.match(/^0b[01]+/i)) { intLiteral = true; } + // Octal + if (stream.match(/^0o[0-7]+/i)) { intLiteral = true; } + // Decimal + if (stream.match(/^[1-9]\d*(e[\+\-]?\d+)?/)) { + // Decimal literals may be "imaginary" + stream.eat(/J/i); + // TODO - Can you have imaginary longs? + intLiteral = true; + } + // Zero by itself with no other piece of number. + if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; } + if (intLiteral) { + // Integer literals may be "long" + stream.eat(/L/i); + return 'number'; + } + } + + var ch = stream.next(); + + if (ch === '$') { + if (stream.eat('{')) { + state.tokenize = tokenVariable; + return tokenVariable(stream, state); + } else { + stream.eatWhile(/[\w\\\-]/); + return 'variable-2'; + } + } + + if (ch === '<' && stream.eat('#')) { + state.tokenize = tokenComment; + return tokenComment(stream, state); + } + + if (ch === '#') { + stream.skipToEnd(); + return 'comment'; + } + + if (ch === '@' && stream.eat('\"')) { + state.tokenize = tokenMultiString; + return tokenMultiString(stream, state); + } + + //if (isOperatorChar.test(ch)) { + // stream.eat; + //stream.next; + // return("variable-1"); + // } + + stream.next(); + return ERRORCLASS; + } + + + function tokenComment(stream, state) { + var maybeEnd = false, ch; + while ((ch = stream.next()) != null) { + if (maybeEnd && ch == ">") { + state.tokenize = tokenBase; + break; + } + maybeEnd = (ch === '#'); + } + return("comment"); + } + + function tokenVariable(stream, state) { + while ((ch = stream.next()) != null) { + if (ch == "}") { + state.tokenize = tokenBase; + break; + } + } + return("variable-2"); + } + + function tokenMultiString(stream, state) { + var maybeEnd = false, ch; + while ((ch = stream.next()) != null) { + if (maybeEnd && ch == "@") { + state.tokenize = tokenBase; + break; + } + maybeEnd = (ch === '"'); + } + return("string"); + } + + function tokenLexer(stream, state) { + //indentInfo = null; + var style = state.tokenize(stream, state); + //var current = stream.current(); + return style; + } + + var external = { + startState: function(basecolumn) { + return { + tokenize: tokenBase, + scopes: [{offset:basecolumn || 0, type:'py'}], + lastToken: null, + lambda: false, + dedent: 0 + }; + }, + + token: function(stream, state) { + var style = tokenLexer(stream, state); + state.lastToken = {style:style, content: stream.current()}; + if (stream.eol() && stream.lambda) { + state.lambda = false; + } + + return style; + }, + + blockCommentStart: "<#", + blockCommentEnd: "#>", + lineComment: "#" + }; + return external; +}); + +CodeMirror.defineMIME("text/x-powershell", "powershell"); -- GitLab