smalltalk.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. CodeMirror.defineMode("smalltalk", function(config, parserConfig) {
  2. var keywords = {"true": 1, "false": 1, nil: 1, self: 1, "super": 1, thisContext: 1};
  3. var indentUnit = config.indentUnit;
  4. function chain(stream, state, f) {
  5. state.tokenize = f;
  6. return f(stream, state);
  7. }
  8. var type;
  9. function ret(tp, style) {
  10. type = tp;
  11. return style;
  12. }
  13. function tokenBase(stream, state) {
  14. var ch = stream.next();
  15. if (ch == '"')
  16. return chain(stream, state, tokenComment(ch));
  17. else if (ch == "'")
  18. return chain(stream, state, tokenString(ch));
  19. else if (ch == "#") {
  20. stream.eatWhile(/[\w\$_]/);
  21. return ret("string", "string");
  22. }
  23. else if(ch == "^") {
  24. return ret("return", "return");
  25. }
  26. else if(/[A-Z]/.test(ch)) {
  27. stream.eatWhile(/[^\s]/);
  28. return ret("className", "className");
  29. }
  30. else if (/\d/.test(ch)) {
  31. stream.eatWhile(/[\w\.]/);
  32. return ret("number", "number");
  33. }
  34. else if (/[\[\]()]/.test(ch)) {
  35. return ret(ch, null);
  36. }
  37. else {
  38. if(ch == ":") {
  39. if(stream.eat("=")) {
  40. return ret("assignment", "assignment");
  41. }
  42. }
  43. stream.eatWhile(/[\w\$_]/);
  44. if (keywords && keywords.propertyIsEnumerable(stream.current())) return ret("keyword", "keyword");
  45. return ret("operator", "operator");
  46. }
  47. }
  48. function tokenString(quote) {
  49. return function(stream, state) {
  50. var escaped = false, next, end = false;
  51. while ((next = stream.next()) != null) {
  52. if (next == quote && !escaped) {end = true; break;}
  53. escaped = !escaped && next == "\\";
  54. }
  55. if (end || !(escaped))
  56. state.tokenize = tokenBase;
  57. return ret("string", "string");
  58. };
  59. }
  60. function tokenComment(quote) {
  61. return function(stream, state) {
  62. var next, end = false;
  63. while ((next = stream.next()) != null) {
  64. if (next == quote) {end = true; break;}
  65. }
  66. if (end)
  67. state.tokenize = tokenBase;
  68. return ret("comment", "comment");
  69. };
  70. }
  71. function Context(indented, column, type, align, prev) {
  72. this.indented = indented;
  73. this.column = column;
  74. this.type = type;
  75. this.align = align;
  76. this.prev = prev;
  77. }
  78. function pushContext(state, col, type) {
  79. return state.context = new Context(state.indented, col, type, null, state.context);
  80. }
  81. function popContext(state) {
  82. return state.context = state.context.prev;
  83. }
  84. // Interface
  85. return {
  86. startState: function(basecolumn) {
  87. return {
  88. tokenize: tokenBase,
  89. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  90. indented: 0,
  91. startOfLine: true
  92. };
  93. },
  94. token: function(stream, state) {
  95. var ctx = state.context;
  96. if (stream.sol()) {
  97. if (ctx.align == null) ctx.align = false;
  98. state.indented = stream.indentation();
  99. state.startOfLine = true;
  100. }
  101. if (stream.eatSpace()) return null;
  102. var style = state.tokenize(stream, state);
  103. if (type == "comment") return style;
  104. if (ctx.align == null) ctx.align = true;
  105. if (type == "[") pushContext(state, stream.column(), "]");
  106. else if (type == "(") pushContext(state, stream.column(), ")");
  107. else if (type == ctx.type) popContext(state);
  108. state.startOfLine = false;
  109. return style;
  110. },
  111. indent: function(state, textAfter) {
  112. if (state.tokenize != tokenBase) return 0;
  113. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type;
  114. if (ctx.align) return ctx.column + (closing ? 0 : 1);
  115. else return ctx.indented + (closing ? 0 : indentUnit);
  116. },
  117. electricChars: "]"
  118. };
  119. });
  120. CodeMirror.defineMIME("text/x-stsrc", {name: "smalltalk"});