You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
8.0 KiB

7 months ago
  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>
  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/5"><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/codemirror5">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).
  59. Currently negative lookbehind assertion for regex is not supported, regardless of browser support.</dd>
  60. <dt><code><strong>token</strong></code>: string | array&lt;string&gt; | null</dt>
  61. <dd>An optional token style. Multiple styles can be specified by
  62. separating them with dots or spaces. When this property holds an array of token styles,
  63. the <code>regex</code> for this rule must capture a group for each array item.
  64. </dd>
  65. <dt><code><strong>sol</strong></code>: boolean</dt>
  66. <dd>When true, this token will only match at the start of the line.
  67. (The <code>^</code> regexp marker doesn't work as you'd expect in
  68. this context because of limitations in JavaScript's RegExp
  69. API.)</dd>
  70. <dt><code><strong>next</strong>: string</code></dt>
  71. <dd>When a <code>next</code> property is present, the mode will
  72. transfer to the state named by the property when the token is
  73. encountered.</dd>
  74. <dt><code><strong>push</strong>: string</code></dt>
  75. <dd>Like <code>next</code>, but instead replacing the current state
  76. by the new state, the current state is kept on a stack, and can be
  77. returned to with the <code>pop</code> directive.</dd>
  78. <dt><code><strong>pop</strong>: bool</code></dt>
  79. <dd>When true, and there is another state on the state stack, will
  80. cause the mode to pop that state off the stack and transition to
  81. it.</dd>
  82. <dt><code><strong>mode</strong>: {spec, end, persistent}</code></dt>
  83. <dd>Can be used to embed another mode inside a mode. When present,
  84. must hold an object with a <code>spec</code> property that describes
  85. the embedded mode, and an optional <code>end</code> end property
  86. that specifies the regexp that will end the extent of the mode. When
  87. a <code>persistent</code> property is set (and true), the nested
  88. mode's state will be preserved between occurrences of the mode.</dd>
  89. <dt><code><strong>indent</strong>: bool</code></dt>
  90. <dd>When true, this token changes the indentation to be one unit
  91. more than the current line's indentation.</dd>
  92. <dt><code><strong>dedent</strong>: bool</code></dt>
  93. <dd>When true, this token will pop one scope off the indentation
  94. stack.</dd>
  95. <dt><code><strong>dedentIfLineStart</strong>: bool</code></dt>
  96. <dd>If a token has its <code>dedent</code> property set, it will, by
  97. default, cause lines where it appears at the start to be dedented.
  98. Set this property to false to prevent that behavior.</dd>
  99. </dl>
  100. <p>The <code>meta</code> property of the states object is special, and
  101. will not be interpreted as a state. Instead, properties set on it will
  102. be set on the mode, which is useful for properties
  103. like <a href="../doc/manual.html#addon_comment"><code>lineComment</code></a>,
  104. which sets the comment style for a mode. The simple mode addon also
  105. recognizes a few such properties:</p>
  106. <dl>
  107. <dt><code><strong>dontIndentStates</strong>: array&lt;string&gt;</code></dt>
  108. <dd>An array of states in which the mode's auto-indentation should
  109. not take effect. Usually used for multi-line comment and string
  110. states.</dd>
  111. </dl>
  112. <script id="modecode">/* Example definition of a simple mode that understands a subset of
  113. * JavaScript:
  114. */
  115. CodeMirror.defineSimpleMode("simplemode", {
  116. // The start state contains the rules that are initially used
  117. start: [
  118. // The regex matches the token, the token property contains the type
  119. {regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string"},
  120. // You can match multiple tokens at once. Note that the captured
  121. // groups must span the whole string in this case
  122. {regex: /(function)(\s+)([a-z$][\w$]*)/,
  123. token: ["keyword", null, "variable-2"]},
  124. // Rules are matched in the order in which they appear, so there is
  125. // no ambiguity between this one and the one above
  126. {regex: /(?:function|var|return|if|for|while|else|do|this)\b/,
  127. token: "keyword"},
  128. {regex: /true|false|null|undefined/, token: "atom"},
  129. {regex: /0x[a-f\d]+|[-+]?(?:\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?/i,
  130. token: "number"},
  131. {regex: /\/\/.*/, token: "comment"},
  132. {regex: /\/(?:[^\\]|\\.)*?\//, token: "variable-3"},
  133. // A next property will cause the mode to move to a different state
  134. {regex: /\/\*/, token: "comment", next: "comment"},
  135. {regex: /[-+\/*=<>!]+/, token: "operator"},
  136. // indent and dedent properties guide autoindentation
  137. {regex: /[\{\[\(]/, indent: true},
  138. {regex: /[\}\]\)]/, dedent: true},
  139. {regex: /[a-z$][\w$]*/, token: "variable"},
  140. // You can embed other modes with the mode property. This rule
  141. // causes all code between << and >> to be highlighted with the XML
  142. // mode.
  143. {regex: /<</, token: "meta", mode: {spec: "xml", end: />>/}}
  144. ],
  145. // The multi-line comment state.
  146. comment: [
  147. {regex: /.*?\*\//, token: "comment", next: "start"},
  148. {regex: /.*/, token: "comment"}
  149. ],
  150. // The meta property contains global information about the mode. It
  151. // can contain properties like lineComment, which are supported by
  152. // all modes, and also directives like dontIndentStates, which are
  153. // specific to simple modes.
  154. meta: {
  155. dontIndentStates: ["comment"],
  156. lineComment: "//"
  157. }
  158. });
  159. </script>
  160. <script>
  161. var sc = document.getElementById("modecode");
  162. var code = document.getElementById("code");
  163. var editor = CodeMirror(code, {
  164. value: (sc.textContent || sc.innerText || sc.innerHTML),
  165. mode: "simplemode"
  166. });
  167. </script>
  168. </article>