##// END OF EJS Templates
Adding FileLink example in the right new place.
Adding FileLink example in the right new place.

File last commit:

r8055:271e17dd
r9199:6d8c0908
Show More
python.js
341 lines | 12.0 KiB | application/javascript | JavascriptLexer
Fernando Perez
Update CodeMirror code to v2.15
r4933 CodeMirror.defineMode("python", function(conf, parserConf) {
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 var ERRORCLASS = 'error';
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 function wordRegexp(words) {
return new RegExp("^((" + words.join(")|(") + "))\\b");
}
Matthias BUSSONNIER
patch SingleOperator in CodeMirror2
r8055
// IPython-specific changes: add '?' as recognized character.
var singleOperators = new RegExp("^[\\+\\-\\*/%&|\\^~<>!\\?]");
// End IPython changes.
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 var singleDelimiters = new RegExp('^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]');
var doubleOperators = new RegExp("^((==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))");
var doubleDelimiters = new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
var tripleDelimiters = new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))");
var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
var wordOperators = wordRegexp(['and', 'or', 'not', 'is', 'in']);
var commonkeywords = ['as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import',
'lambda', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield'];
Brian Granger
Updating CodeMirror to latest to incorporate bug fixes.
r5970 var commonBuiltins = ['abs', 'all', 'any', 'bin', 'bool', 'bytearray', 'callable', 'chr',
'classmethod', 'compile', 'complex', 'delattr', 'dict', 'dir', 'divmod',
'enumerate', 'eval', 'filter', 'float', 'format', 'frozenset',
'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id',
'input', 'int', 'isinstance', 'issubclass', 'iter', 'len',
'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next',
'object', 'oct', 'open', 'ord', 'pow', 'property', 'range',
'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
'type', 'vars', 'zip', '__import__', 'NotImplemented',
'Ellipsis', '__debug__'];
var py2 = {'builtins': ['apply', 'basestring', 'buffer', 'cmp', 'coerce', 'execfile',
'file', 'intern', 'long', 'raw_input', 'reduce', 'reload',
'unichr', 'unicode', 'xrange', 'False', 'True', 'None'],
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 'keywords': ['exec', 'print']};
Brian Granger
Updating CodeMirror to latest to incorporate bug fixes.
r5970 var py3 = {'builtins': ['ascii', 'bytes', 'exec', 'print'],
'keywords': ['nonlocal', 'False', 'True', 'None']};
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504
Fernando Perez
Update CodeMirror code to v2.15
r4933 if (!!parserConf.version && parseInt(parserConf.version, 10) === 3) {
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 commonkeywords = commonkeywords.concat(py3.keywords);
Brian Granger
Updating CodeMirror to latest to incorporate bug fixes.
r5970 commonBuiltins = commonBuiltins.concat(py3.builtins);
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 var stringPrefixes = new RegExp("^(([rb]|(br))?('{3}|\"{3}|['\"]))", "i");
} else {
commonkeywords = commonkeywords.concat(py2.keywords);
Brian Granger
Updating CodeMirror to latest to incorporate bug fixes.
r5970 commonBuiltins = commonBuiltins.concat(py2.builtins);
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 var stringPrefixes = new RegExp("^(([rub]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i");
}
var keywords = wordRegexp(commonkeywords);
Brian Granger
Updating CodeMirror to latest to incorporate bug fixes.
r5970 var builtins = wordRegexp(commonBuiltins);
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504
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;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 var ch = stream.peek();
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Handle Comments
if (ch === '#') {
stream.skipToEnd();
return 'comment';
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // 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';
}
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Handle Strings
if (stream.match(stringPrefixes)) {
state.tokenize = tokenStringFactory(stream.current());
return state.tokenize(stream, state);
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Handle operators and Delimiters
if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
return null;
}
if (stream.match(doubleOperators)
|| stream.match(singleOperators)
|| stream.match(wordOperators)) {
return 'operator';
}
if (stream.match(singleDelimiters)) {
return null;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 if (stream.match(keywords)) {
return 'keyword';
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian Granger
Updating CodeMirror to latest to incorporate bug fixes.
r5970 if (stream.match(builtins)) {
return 'builtin';
}
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 if (stream.match(identifiers)) {
return 'variable';
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Handle non-detected items
stream.next();
return ERRORCLASS;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 function tokenStringFactory(delimiter) {
Fernando Perez
Update CodeMirror code to v2.15
r4933 while ('rub'.indexOf(delimiter.charAt(0).toLowerCase()) >= 0) {
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 delimiter = delimiter.substr(1);
}
var singleline = delimiter.length == 1;
var OUTCLASS = 'string';
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 return function tokenString(stream, state) {
while (!stream.eol()) {
stream.eatWhile(/[^'"\\]/);
if (stream.eat('\\')) {
stream.next();
if (singleline && stream.eol()) {
return OUTCLASS;
}
Fernando Perez
Update CodeMirror code to v2.15
r4933 } else if (stream.match(delimiter)) {
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 state.tokenize = tokenBase;
return OUTCLASS;
} else {
stream.eat(/['"]/);
}
}
if (singleline) {
Fernando Perez
Update CodeMirror code to v2.15
r4933 if (parserConf.singleLineStringErrors) {
return ERRORCLASS;
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 } else {
state.tokenize = tokenBase;
}
}
return OUTCLASS;
};
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 function indent(stream, state, type) {
type = type || 'py';
var indentUnit = 0;
if (type === 'py') {
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941 if (state.scopes[0].type !== 'py') {
state.scopes[0].offset = stream.indentation();
return;
}
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 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
});
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
function dedent(stream, state, type) {
type = type || 'py';
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 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 {
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941 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;
}
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 }
}
function tokenLexer(stream, state) {
indentInfo = null;
var style = state.tokenize(stream, state);
var current = stream.current();
// Handle '.' connected identifiers
if (current === '.') {
Matthias BUSSONNIER
update CodeMirror2 to 2.32
r8053 style = stream.match(identifiers, false) ? null : ERRORCLASS;
if (style === null && state.lastToken === 'meta') {
// Apply 'meta' style to '.' connected identifiers when
// appropriate.
style = 'meta';
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 }
Matthias BUSSONNIER
update CodeMirror2 to 2.32
r8053 return style;
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 }
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Handle decorators
if (current === '@') {
Matthias BUSSONNIER
update CodeMirror2 to 2.32
r8053 return stream.match(identifiers, false) ? 'meta' : ERRORCLASS;
}
if ((style === 'variable' || style === 'builtin')
&& state.lastToken === 'meta') {
style = 'meta';
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 }
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Handle scope changes.
if (current === 'pass' || current === 'return') {
state.dedent += 1;
}
Matthias BUSSONNIER
update CodeMirror2 to 2.32
r8053 if (current === 'lambda') state.lambda = true;
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 if ((current === ':' && !state.lambda && state.scopes[0].type == 'py')
|| indentInfo === 'indent') {
indent(stream, state);
}
var delimiter_index = '[({'.indexOf(current);
if (delimiter_index !== -1) {
indent(stream, state, '])}'.slice(delimiter_index, delimiter_index+1));
}
if (indentInfo === 'dedent') {
if (dedent(stream, state)) {
return ERRORCLASS;
}
}
delimiter_index = '])}'.indexOf(current);
if (delimiter_index !== -1) {
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941 if (dedent(stream, state, current)) {
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 return ERRORCLASS;
}
}
if (state.dedent > 0 && stream.eol() && state.scopes[0].type == 'py') {
if (state.scopes.length > 1) state.scopes.shift();
state.dedent -= 1;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 return style;
}
var external = {
startState: function(basecolumn) {
return {
tokenize: tokenBase,
scopes: [{offset:basecolumn || 0, type:'py'}],
lastToken: null,
lambda: false,
dedent: 0
};
},
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 token: function(stream, state) {
var style = tokenLexer(stream, state);
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Matthias BUSSONNIER
update CodeMirror2 to 2.32
r8053 state.lastToken = style;
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 if (stream.eol() && stream.lambda) {
state.lambda = false;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 return style;
},
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 indent: function(state, textAfter) {
if (state.tokenize != tokenBase) {
return 0;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 return state.scopes[0].offset;
}
Brian Granger
Updating to CodeMirror 2.2, latest stable release.
r5941
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 };
return external;
});
CodeMirror.defineMIME("text/x-python", "python");