Created
September 10, 2013 15:55
-
-
Save mgaitan/6511522 to your computer and use it in GitHub Desktop.
first attempt of codemirror fortran parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CodeMirror.defineMode("fortran", function(conf, parserConf) { | |
| var ERRORCLASS = 'error'; | |
| function wordRegexp(words) { | |
| return new RegExp("^((" + words.join(")|(") + "))\\b", "i"); | |
| } | |
| var Operators = new RegExp("(\.and\.|\.or\.|\.eq\.|\.lt\.|\.le\.|\.gt\.|\.ge\.|\.ne\.|\.not\.|\.eqv\.|\.neqv\.)", "i"); | |
| var Operators2 = new RegExp("((==)|(/=)|(=)|(<=)|(>=)|(/)|(//))"); //" |(\\*\\*)|\-|\+|\/\/|\/|(?!^)\*)"); | |
| var singleDelimiters = new RegExp('^[\\(\\)\\[\\]\\,:=]') | |
| var stringPrefixes = new RegExp("^(([rub]|(ur)|(br))?(['\"]))", "i"); | |
| var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*"); | |
| var commonkeywords = ["abstract", "accept", "allocatable", "allocate", | |
| "array", "assign", "asynchronous", "backspace", | |
| "bind", "block( data)?", "byte", "call", "case", | |
| "class", "close", "common", "contains", | |
| "continue", "cycle", "data", "deallocate", | |
| "decode", "deferred", "dimension", "do", | |
| "elemental", "else", "encode", "end( file)?", | |
| "endif", "entry", "enumerator", "equivalence", | |
| "exit", "external", "extrinsic", "final", | |
| "forall", "format", "function", "generic", | |
| "goto", "if", "implicit", "import", "include", | |
| "inquire", "intent", "interface", "intrinsic", | |
| "module", "namelist", "non_intrinsic", | |
| "non_overridable", "none", "nopass", | |
| "nullify", "open", "optional", "options", | |
| "parameter", "pass", "pause", "pointer", | |
| "print", "private", "program", "protected", | |
| "public", "pure", "read", "recursive", "result", | |
| "return", "rewind", "save", "select", "sequence", | |
| "stop", "subroutine", "target", "then", "type", | |
| "use", "value", "volatile", "where", "while", | |
| "write"]; | |
| var blockskeywords = ["program", "subroutine", "module", "type", "call", "block"]; | |
| var dataTypes = ["c_bool", "c_char", "c_double", "c_double_complex", | |
| "c_float", "c_float_complex", "c_funptr", "c_int", | |
| "c_int16_t", "c_int32_t", "c_int64_t", "c_int8_t", | |
| "c_int_fast16_t", "c_int_fast32_t", "c_int_fast64_t", | |
| "c_int_fast8_t", "c_int_least16_t", "c_int_least32_t", | |
| "c_int_least64_t", "c_int_least8_t", "c_intmax_t", | |
| "c_intptr_t", "c_long", "c_long_double", | |
| "c_long_double_complex", "c_long_long", "c_ptr", | |
| "c_short", "c_signed_char", "c_size_t", "character", | |
| "complex", "double complex", "double precision", | |
| "integer", "logical", "real"]; | |
| var commonBuiltins = ["abort", "abs", "access", "achar", "acos", | |
| "adjustl", "adjustr", "aimag", "aint", "alarm", | |
| "all", "allocated", "alog", "amax", "amin", | |
| "amod", "and", "anint", "any", "asin", | |
| "associated", "atan", "besj", "besjn", "besy", | |
| "besyn", "bit_size", "btest", "cabs", "ccos", | |
| "ceiling", "cexp", "char", "chdir", "chmod", | |
| "clog", "cmplx", "command_argument_count", | |
| "complex", "conjg", "cos", "cosh", "count", | |
| "cpu_time", "cshift", "csin", "csqrt", "ctime", | |
| "c_funloc", "c_loc", "c_associated", "c_null_ptr", | |
| "c_null_funptr", "c_f_pointer", "c_null_char", | |
| "c_alert", "c_backspace", "c_form_feed", | |
| "c_new_line", "c_carriage_return", | |
| "c_horizontal_tab", "c_vertical_tab", "dabs", | |
| "dacos", "dasin", "datan", "date_and_time", | |
| "dbesj", "dbesj", "dbesjn", "dbesy", "dbesy", | |
| "dbesyn", "dble", "dcos", "dcosh", "ddim", "derf", | |
| "derfc", "dexp", "digits", "dim", "dint", "dlog", | |
| "dlog", "dmax", "dmin", "dmod", "dnint", | |
| "dot_product", "dprod", "dsign", "dsinh", | |
| "dsin", "dsqrt", "dtanh", "dtan", "dtime", | |
| "eoshift", "epsilon", "erf", "erfc", "etime", | |
| "exit", "exp", "exponent", "extends_type_of", | |
| "fdate", "fget", "fgetc", "float", "floor", | |
| "flush", "fnum", "fputc", "fput", "fraction", | |
| "fseek", "fstat", "ftell", "gerror", "getarg", | |
| "get_command", "get_command_argument", | |
| "get_environment_variable", "getcwd", | |
| "getenv", "getgid", "getlog", "getpid", | |
| "getuid", "gmtime", "hostnm", "huge", "iabs", | |
| "iachar", "iand", "iargc", "ibclr", "ibits", | |
| "ibset", "ichar", "idate", "idim", "idint", | |
| "idnint", "ieor", "ierrno", "ifix", "imag", | |
| "imagpart", "index", "int", "ior", "irand", | |
| "isatty", "ishft", "ishftc", "isign", | |
| "iso_c_binding", "is_iostat_end", "is_iostat_eor", | |
| "itime", "kill", "kind", "lbound", "len", "len_trim", | |
| "lge", "lgt", "link", "lle", "llt", "lnblnk", "loc", | |
| "log", "logical", "long", "lshift", "lstat", "ltime", | |
| "matmul", "max", "maxexponent", "maxloc", "maxval", | |
| "mclock", "merge", "move_alloc", "min", "minexponent", | |
| "minloc", "minval", "mod", "modulo", "mvbits", | |
| "nearest", "new_line", "nint", "not", "or", "pack", | |
| "perror", "precision", "present", "product", "radix", | |
| "rand", "random_number", "random_seed", "range", | |
| "real", "realpart", "rename", "repeat", "reshape", | |
| "rrspacing", "rshift", "same_type_as", "scale", | |
| "scan", "second", "selected_int_kind", | |
| "selected_real_kind", "set_exponent", "shape", | |
| "short", "sign", "signal", "sinh", "sin", "sleep", | |
| "sngl", "spacing", "spread", "sqrt", "srand", "stat", | |
| "sum", "symlnk", "system", "system_clock", "tan", | |
| "tanh", "time", "tiny", "transfer", "transpose", | |
| "trim", "ttynam", "ubound", "umask", "unlink", | |
| "unpack", "verify", "xor", "zabs", "zcos", "zexp", | |
| "zlog", "zsin", "zsqrt"]; | |
| var keywords = wordRegexp(commonkeywords.concat(dataTypes)); | |
| var builtins = wordRegexp(commonBuiltins); | |
| var blocks = wordRegexp(blockskeywords); | |
| var indentInfo = null; | |
| // tokenizers | |
| function tokenBase(stream, state) { | |
| // Handle scope changes | |
| if (stream.sol()) { | |
| var scopeOffset = state.scopes[0].offset; | |
| if (stream.eatSpace()) { | |
| var lineOffset = stream.indentation(); | |
| if (lineOffset > scopeOffset) { | |
| indentInfo = 'indent'; | |
| } else if (lineOffset < scopeOffset) { | |
| indentInfo = 'dedent'; | |
| } | |
| return null; | |
| } else { | |
| if (scopeOffset > 0) { | |
| dedent(stream, state); | |
| } | |
| } | |
| } | |
| if (stream.eatSpace()) { | |
| return null; | |
| } | |
| var ch = stream.peek(); | |
| // Handle Comments | |
| if (ch === '!') { | |
| stream.skipToEnd(); | |
| return 'comment'; | |
| } | |
| // Handle Numbers Literals | |
| if (stream.match(/\b[\+\-]?[0-9]+\.?[0-9a-zA-Z_]*\b/, false)) { | |
| return 'number'; | |
| } | |
| // Handle Strings | |
| if (stream.match(stringPrefixes)) { | |
| state.tokenize = tokenStringFactory(stream.current()); | |
| return state.tokenize(stream, state); | |
| } | |
| if (stream.match(Operators) || stream.match(Operators2)){ | |
| return 'operator'; | |
| } | |
| if (stream.match(singleDelimiters)) { | |
| return null; | |
| } | |
| if (stream.match(keywords)) { | |
| return 'keyword'; | |
| } | |
| if (stream.match(builtins)) { | |
| return 'builtin'; | |
| } | |
| if (stream.match(identifiers)) { | |
| if (state.lastToken.match(blocks)){ | |
| return 'def'; | |
| } | |
| return 'variable'; | |
| } | |
| // Handle non-detected items | |
| stream.next(); | |
| return ERRORCLASS; | |
| } | |
| function tokenStringFactory(delimiter) { | |
| while ('rub'.indexOf(delimiter.charAt(0).toLowerCase()) >= 0) { | |
| delimiter = delimiter.substr(1); | |
| } | |
| var singleline = delimiter.length == 1; | |
| var OUTCLASS = 'string'; | |
| function tokenString(stream, state) { | |
| while (!stream.eol()) { | |
| stream.eatWhile(/[^'"\\]/); | |
| if (stream.eat('\\')) { | |
| stream.next(); | |
| if (singleline && stream.eol()) { | |
| return OUTCLASS; | |
| } | |
| } else if (stream.match(delimiter)) { | |
| state.tokenize = tokenBase; | |
| return OUTCLASS; | |
| } else { | |
| stream.eat(/['"]/); | |
| } | |
| } | |
| if (singleline) { | |
| if (parserConf.singleLineStringErrors) { | |
| return ERRORCLASS; | |
| } else { | |
| state.tokenize = tokenBase; | |
| } | |
| } | |
| return OUTCLASS; | |
| } | |
| tokenString.isString = true; | |
| return tokenString; | |
| } | |
| function indent(stream, state, type) { | |
| type = type || 'py'; | |
| var indentUnit = 0; | |
| if (type === 'py') { | |
| if (state.scopes[0].type !== 'py') { | |
| state.scopes[0].offset = stream.indentation(); | |
| return; | |
| } | |
| for (var i = 0; i < state.scopes.length; ++i) { | |
| if (state.scopes[i].type === 'py') { | |
| indentUnit = state.scopes[i].offset + conf.indentUnit; | |
| break; | |
| } | |
| } | |
| } else { | |
| indentUnit = stream.column() + stream.current().length; | |
| } | |
| state.scopes.unshift({ | |
| offset: indentUnit, | |
| type: type | |
| }); | |
| } | |
| function dedent(stream, state, type) { | |
| type = type || 'py'; | |
| if (state.scopes.length == 1) return; | |
| if (state.scopes[0].type === 'py') { | |
| var _indent = stream.indentation(); | |
| var _indent_index = -1; | |
| for (var i = 0; i < state.scopes.length; ++i) { | |
| if (_indent === state.scopes[i].offset) { | |
| _indent_index = i; | |
| break; | |
| } | |
| } | |
| if (_indent_index === -1) { | |
| return true; | |
| } | |
| while (state.scopes[0].offset !== _indent) { | |
| state.scopes.shift(); | |
| } | |
| return false; | |
| } else { | |
| if (type === 'py') { | |
| state.scopes[0].offset = stream.indentation(); | |
| return false; | |
| } else { | |
| if (state.scopes[0].type != type) { | |
| return true; | |
| } | |
| state.scopes.shift(); | |
| return false; | |
| } | |
| } | |
| } | |
| var external = { | |
| startState: function(basecolumn) { | |
| return { | |
| tokenize: tokenBase, | |
| scopes: [{offset:basecolumn || 0, type:'py'}], | |
| lastStyle: null, | |
| lastToken: null, | |
| lambda: false, | |
| dedent: 0 | |
| }; | |
| }, | |
| token: function(stream, state) { | |
| var style = state.tokenize(stream, state); | |
| state.lastStyle = style; | |
| var current = stream.current(); | |
| if (current && style) { | |
| state.lastToken = current; | |
| } | |
| if (stream.eol() && state.lambda) { | |
| state.lambda = false; | |
| } | |
| return style; | |
| }, | |
| indent: function(state) { | |
| if (state.tokenize != tokenBase) { | |
| return state.tokenize.isString ? CodeMirror.Pass : 0; | |
| } | |
| return state.scopes[0].offset; | |
| }, | |
| lineComment: "!" | |
| }; | |
| return external; | |
| }); | |
| CodeMirror.defineMIME("text/x-fortran", "fortran"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment