simplemode.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!doctype html>
  2. <title>CodeMirror: Simple Mode Demo</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../doc/docs.css">
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <script src="../lib/codemirror.js"></script>
  7. <script src="../addon/mode/simple.js"></script>
  8. <script src="../mode/xml/xml.js"></script>
  9. <style type="text/css">
  10. .CodeMirror {border: 1px solid silver; margin-bottom: 1em; }
  11. dt { text-indent: -2em; padding-left: 2em; margin-top: 1em; }
  12. dd { margin-left: 1.5em; margin-bottom: 1em; }
  13. dt {margin-top: 1em;}
  14. </style>
  15. <div id=nav>
  16. <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  17. <ul>
  18. <li><a href="../index.html">Home</a>
  19. <li><a href="../doc/manual.html">Manual</a>
  20. <li><a href="https://github.com/codemirror/codemirror">Code</a>
  21. </ul>
  22. <ul>
  23. <li><a class=active href="#">Simple Mode</a>
  24. </ul>
  25. </div>
  26. <article>
  27. <h2>Simple Mode Demo</h2>
  28. <p>The <a href="../addon/mode/simple.js"><code>mode/simple</code></a>
  29. addon allows CodeMirror modes to be specified using a relatively simple
  30. declarative format. This format is not as powerful as writing code
  31. directly against the <a href="../doc/manual.html#modeapi">mode
  32. interface</a>, but is a lot easier to get started with, and
  33. sufficiently expressive for many simple language modes.</p>
  34. <p>This interface is still in flux. It is unlikely to be scrapped or
  35. overhauled completely, so do start writing code against it, but
  36. details might change as it stabilizes, and you might have to tweak
  37. your code when upgrading.</p>
  38. <p>Simple modes (loosely based on
  39. the <a href="https://github.com/mozilla/skywriter/wiki/Common-JavaScript-Syntax-Highlighting-Specification">Common
  40. JavaScript Syntax Highlighting Specification</a>, which never took
  41. off), are state machines, where each state has a number of rules that
  42. match tokens. A rule describes a type of token that may occur in the
  43. current state, and possibly a transition to another state caused by
  44. that token.</p>
  45. <p>The <code>CodeMirror.defineSimpleMode(name, states)</code> method
  46. takes a mode name and an object that describes the mode's states. The
  47. editor below shows an example of such a mode (and is itself
  48. highlighted by the mode shown in it).</p>
  49. <div id="code"></div>
  50. <p>Each state is an array of rules. A rule may have the following properties:</p>
  51. <dl>
  52. <dt><code><strong>regex</strong>: string | RegExp</code></dt>
  53. <dd>The regular expression that matches the token. May be a string
  54. or a regex object. When a regex, the <code>ignoreCase</code> flag
  55. will be taken into account when matching the token. This regex
  56. has to capture groups when the <code>token</code> property is
  57. an array. If it captures groups, it must capture <em>all</em> of the string
  58. (since JS provides no way to find out where a group matched).</dd>
  59. <dt><code><strong>token</strong></code>: string | array&lt;string&gt; | null</dt>
  60. <dd>An optional token style. Multiple styles can be specified by
  61. separating them with dots or spaces. When this property holds an array of token styles,
  62. the <code>regex</code> for this rule must capture a group for each array item.
  63. </dd>
  64. <dt><code><strong>sol</strong></code>: boolean</dt>
  65. <dd>When true, this token will only match at the start of the line.
  66. (The <code>^</code> regexp marker doesn't work as you'd expect in
  67. this context because of limitations in JavaScript's RegExp
  68. API.)</dd>
  69. <dt><code><strong>next</strong>: string</code></dt>
  70. <dd>When a <code>next</code> property is present, the mode will
  71. transfer to the state named by the property when the token is
  72. encountered.</dd>
  73. <dt><code><strong>push</strong>: string</code></dt>
  74. <dd>Like <code>next</code>, but instead replacing the current state
  75. by the new state, the current state is kept on a stack, and can be
  76. returned to with the <code>pop</code> directive.</dd>
  77. <dt><code><strong>pop</strong>: bool</code></dt>
  78. <dd>When true, and there is another state on the state stack, will
  79. cause the mode to pop that state off the stack and transition to
  80. it.</dd>
  81. <dt><code><strong>mode</strong>: {spec, end, persistent}</code></dt>
  82. <dd>Can be used to embed another mode inside a mode. When present,
  83. must hold an object with a <code>spec</code> property that describes
  84. the embedded mode, and an optional <code>end</code> end property
  85. that specifies the regexp that will end the extent of the mode. When
  86. a <code>persistent</code> property is set (and true), the nested
  87. mode's state will be preserved between occurrences of the mode.</dd>
  88. <dt><code><strong>indent</strong>: bool</code></dt>
  89. <dd>When true, this token changes the indentation to be one unit
  90. more than the current line's indentation.</dd>
  91. <dt><code><strong>dedent</strong>: bool</code></dt>
  92. <dd>When true, this token will pop one scope off the indentation
  93. stack.</dd>
  94. <dt><code><strong>dedentIfLineStart</strong>: bool</code></dt>
  95. <dd>If a token has its <code>dedent</code> property set, it will, by
  96. default, cause lines where it appears at the start to be dedented.
  97. Set this property to false to prevent that behavior.</dd>
  98. </dl>
  99. <p>The <code>meta</code> property of the states object is special, and
  100. will not be interpreted as a state. Instead, properties set on it will
  101. be set on the mode, which is useful for properties
  102. like <a href="../doc/manual.html#addon_comment"><code>lineComment</code></a>,
  103. which sets the comment style for a mode. The simple mode addon also
  104. recognizes a few such properties:</p>
  105. <dl>
  106. <dt><code><strong>dontIndentStates</strong>: array&lt;string&gt;</code></dt>
  107. <dd>An array of states in which the mode's auto-indentation should
  108. not take effect. Usually used for multi-line comment and string
  109. states.</dd>
  110. </dl>
  111. <script id="modecode">/* Example definition of a simple mode that understands a subset of
  112. * JavaScript:
  113. */
  114. CodeMirror.defineSimpleMode("simplemode", {
  115. // The start state contains the rules that are intially used
  116. start: [
  117. // The regex matches the token, the token property contains the type
  118. {regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string"},
  119. // You can match multiple tokens at once. Note that the captured
  120. // groups must span the whole string in this case
  121. {regex: /(function)(\s+)([a-z$][\w$]*)/,
  122. token: ["keyword", null, "variable-2"]},
  123. // Rules are matched in the order in which they appear, so there is
  124. // no ambiguity between this one and the one above
  125. {regex: /(?:function|var|return|if|for|while|else|do|this)\b/,
  126. token: "keyword"},
  127. {regex: /true|false|null|undefined/, token: "atom"},
  128. {regex: /0x[a-f\d]+|[-+]?(?:\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?/i,
  129. token: "number"},
  130. {regex: /\/\/.*/, token: "comment"},
  131. {regex: /\/(?:[^\\]|\\.)*?\//, token: "variable-3"},
  132. // A next property will cause the mode to move to a different state
  133. {regex: /\/\*/, token: "comment", next: "comment"},
  134. {regex: /[-+\/*=<>!]+/, token: "operator"},
  135. // indent and dedent properties guide autoindentation
  136. {regex: /[\{\[\(]/, indent: true},
  137. {regex: /[\}\]\)]/, dedent: true},
  138. {regex: /[a-z$][\w$]*/, token: "variable"},
  139. // You can embed other modes with the mode property. This rule
  140. // causes all code between << and >> to be highlighted with the XML
  141. // mode.
  142. {regex: /<</, token: "meta", mode: {spec: "xml", end: />>/}}
  143. ],
  144. // The multi-line comment state.
  145. comment: [
  146. {regex: /.*?\*\//, token: "comment", next: "start"},
  147. {regex: /.*/, token: "comment"}
  148. ],
  149. // The meta property contains global information about the mode. It
  150. // can contain properties like lineComment, which are supported by
  151. // all modes, and also directives like dontIndentStates, which are
  152. // specific to simple modes.
  153. meta: {
  154. dontIndentStates: ["comment"],
  155. lineComment: "//"
  156. }
  157. });
  158. </script>
  159. <script>
  160. var sc = document.getElementById("modecode");
  161. var code = document.getElementById("code");
  162. var editor = CodeMirror(code, {
  163. value: (sc.textContent || sc.innerText || sc.innerHTML),
  164. mode: "simplemode"
  165. });
  166. </script>
  167. </article>