ui-code-editor.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. YUI.add('ui-code-editor', function (Y) {
  2. CodeMirror.defineMode("oberon07", function () {
  3. function words(str) {
  4. var obj = {}, words = str.split(" ");
  5. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  6. return obj;
  7. }
  8. var keywords = words("ARRAY IMPORT THEN BEGIN IN TO BY IS TRUE CASE MOD TYPE " +
  9. "CONST MODULE UNTIL DIV NIL VAR DO OF WHILE ELSE OR " +
  10. "ELSIF POINTER END PROCEDURE FALSE RECORD FOR REPEAT IF RETURN");
  11. var atoms = {'null': true};
  12. var isOperatorChar = /[+\-*&%=<>^~#!?\|\/]/;
  13. function tokenBase(stream, state) {
  14. var ch = stream.next();
  15. if (ch == "#" && state.startOfLine) {
  16. stream.skipToEnd();
  17. return "meta";
  18. }
  19. if (ch == '"' || ch == "'") {
  20. state.tokenize = tokenString(ch);
  21. return state.tokenize(stream, state);
  22. }
  23. if (ch == "(" && stream.eat("*")) {
  24. state.tokenize = tokenComment;
  25. return tokenComment(stream, state);
  26. }
  27. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  28. return null;
  29. }
  30. if (/\d/.test(ch)) {
  31. stream.eatWhile(/[\w\.]/);
  32. return "number";
  33. }
  34. if (ch == "/") {
  35. if (stream.eat("/")) {
  36. stream.skipToEnd();
  37. return "comment";
  38. }
  39. }
  40. if (isOperatorChar.test(ch)) {
  41. stream.eatWhile(isOperatorChar);
  42. return "operator";
  43. }
  44. stream.eatWhile(/[\w\$_]/);
  45. var cur = stream.current();
  46. if (keywords.propertyIsEnumerable(cur)) return "keyword";
  47. if (atoms.propertyIsEnumerable(cur)) return "atom";
  48. return "variable";
  49. }
  50. function tokenString(quote) {
  51. return function(stream, state) {
  52. var escaped = false, next, end = false;
  53. while ((next = stream.next()) != null) {
  54. if (next == quote && !escaped) {end = true; break;}
  55. escaped = !escaped && next == "\\";
  56. }
  57. if (end || !escaped) state.tokenize = null;
  58. return "string";
  59. };
  60. }
  61. function tokenComment(stream, state) {
  62. var maybeEnd = false, ch;
  63. while (ch = stream.next()) {
  64. if (ch == ")" && maybeEnd) {
  65. state.tokenize = null;
  66. break;
  67. }
  68. maybeEnd = (ch == "*");
  69. }
  70. return "comment";
  71. }
  72. // Interface
  73. return {
  74. startState: function() {
  75. return {tokenize: null};
  76. },
  77. token: function(stream, state) {
  78. if (stream.eatSpace()) return null;
  79. var style = (state.tokenize || tokenBase)(stream, state);
  80. if (style == "comment" || style == "meta") return style;
  81. return style;
  82. },
  83. electricChars: "{}"
  84. };
  85. });
  86. CodeMirror.defineMIME("text/x-oberon07", "oberon07");
  87. /** Wrapper for CodeMirror Editor */
  88. Y.namespace('UI.Code').Editor = Y.Base.create('editorCodeUI', Y.View, {
  89. render: function (srcNode) {
  90. var editor,
  91. node = srcNode instanceof Y.Node ? srcNode : Y.one(srcNode);
  92. if (node) {
  93. editor = CodeMirror.fromTextArea(node._node, {
  94. lineNumbers: true,
  95. mode: 'text/x-oberon07'
  96. });
  97. }
  98. }
  99. }, {});
  100. }, '1.0', {
  101. requires: [
  102. 'view'
  103. ]
  104. });