Simple Mode Demo简单模式

The mode/simple addon allows CodeMirror modes to be specified using a relatively simple declarative format. mode/simple附件允许利用一些相对简单的声明格式来指定CodeMirror模式。This format is not as powerful as writing code directly against the mode interface, but is a lot easier to get started with, and sufficiently expressive for many simple language modes.此模式并不如直接针对模式接口编写代码强大,但是很容易开始,对许多简单的语言模式都有足够的表现力。

This interface is still in flux. 接口依然在变化中。It is unlikely to be scrapped or overhauled completely, so do start writing code against it, but details might change as it stabilizes, and you might have to tweak your code when upgrading.它不太可能被完全废弃或彻底检修,所以针对它开始编写代码,但是细节可能会随着它的稳定而改变,升级时您可能需要调整代码。

Simple modes (loosely based on the Common JavaScript Syntax Highlighting Specification, which never took off), are state machines, where each state has a number of rules that match tokens. 简单模式(松散基于常见JavaScript语法高亮规范文档,它从未被脱离过)是语句匹配,其中每个语句具有一些匹配口令的规则。A rule describes a type of token that may occur in the current state, and possibly a transition to another state caused by that token.规则描述了当前状态下可能发生的口令类型,而且该口令可能导致的到另一个状态的转换。

The CodeMirror.defineSimpleMode(name, states) method takes a mode name and an object that describes the mode's states. CodeMirror.defineSimpleMode(name, states)方法取用一个模式名称,以及一个对象,描述了模式的状态。The editor below shows an example of such a mode (and is itself highlighted by the mode shown in it).下面的编辑器显示了这种模式的示例(自身也被它里面显示的模式高亮)。

Each state is an array of rules. 每种状态都是规则的数组。A rule may have the following properties:每个规则可能具有以下的属性:

regex: string | RegExp
The regular expression that matches the token. 匹配口令的正则表达式。May be a string or a regex object. 可以是一个字符串,或者是regexp对象。When a regex, the ignoreCase flag will be taken into account when matching the token. 如果是regexp对象,则在匹配口令时将考虑到ignoreCase标记。This regex has to capture groups when the token property is an array. 如果token属性是数组,则regexp不得不匹配组。If it captures groups, it must capture all of the string (since JS provides no way to find out where a group matched).如果它捕获了组,它必须捕获所有字符串(因为JS提供不了查找匹配位置的方法)。
token: string | array<string> | null
An optional token style. 多个可选的口令样式。Multiple styles can be specified by separating them with dots or spaces. 可以指定多个样式,用点或者空格隔开它们。When this property holds an array of token styles, the regex for this rule must capture a group for each array item.如果所控住的属性是一个口令样式的数组,则针对此规则的regex必须针对数组中的每一项捕获一个组。
sol: boolean
When true, this token will only match at the start of the line. (The ^ regexp marker doesn't work as you'd expect in this context because of limitations in JavaScript's RegExp API.)
next: string
When a next property is present, the mode will transfer to the state named by the property when the token is encountered.
push: string
Like next, but instead replacing the current state by the new state, the current state is kept on a stack, and can be returned to with the pop directive.
pop: bool
When true, and there is another state on the state stack, will cause the mode to pop that state off the stack and transition to it.
mode: {spec, end, persistent}
Can be used to embed another mode inside a mode. When present, must hold an object with a spec property that describes the embedded mode, and an optional end end property that specifies the regexp that will end the extent of the mode. When a persistent property is set (and true), the nested mode's state will be preserved between occurrences of the mode.
indent: bool
When true, this token changes the indentation to be one unit more than the current line's indentation.
dedent: bool
When true, this token will pop one scope off the indentation stack.
dedentIfLineStart: bool
If a token has its dedent property set, it will, by default, cause lines where it appears at the start to be dedented. Set this property to false to prevent that behavior.

The meta property of the states object is special, and will not be interpreted as a state. Instead, properties set on it will be set on the mode, which is useful for properties like lineComment, which sets the comment style for a mode. The simple mode addon also recognizes a few such properties:

dontIndentStates: array<string>
An array of states in which the mode's auto-indentation should not take effect. Usually used for multi-line comment and string states.