我应该如何为这种简单的玩具语言创建Vim语法?

时间:2011-06-27 19:38:35

标签: vim syntax-highlighting

我有一种非常简单的玩具语言,我想创建一个Vim语法。

// A non-functional sample:

// "resolve" is the keyword here, though that's not a fixed part of the
// syntax.
$hostsys : resolve "host" "system"
// Since there's only one token to the right of the = here, it's an alias
// and not an operation.
$zero = 0
// An operation is analogous to a method call in a typical OO language.
// "toString" would be the keyword or method name here
$str = toString $someObject
// It's possible for there to be more or fewer than one lvalue on a
// directive or operation.
// 2 return values
$xp $yp = rotate $util $x $y 90
// No return values
= println $out $str

我似乎遇到的主要问题是该语言没有固定的关键字集,而是每行某个位置的令牌被视为关键字。我不知道在哪里寻找这个例子。

以下是语法的完整概述:

program : line*

line : <start of line> WS* command? comment? <end of line>

command : operation | alias | directive

// Always at least one operand after operator
operation : (Register WS+)* '=' WS+ operator (WS+ operand)+ WS*

// No operator and only one "operand"
alias : Register WS+ '=' WS+ operand <not followed by WS+ operand> WS*

directive : (Register WS+)* ':' WS+ operator (WS+ operand)* WS*

operator : QuotedString | Register | bareword

operand : value

WS : /\s/

QuotedString : /"[^"]*"/

Register : /\$\S+/

BooleanLiteral : /true|false/

NoneLiteral : /none/

NumericLiteral : /-?\d+(\.\d+)?/

nonBarewordValue :
    QuotedString | Register | BooleanLiteral | NoneLiteral | NumericLiteral

Comment : "//" <any non-newline character>*

bareword : /\S+/ except {Comment, nonBarewordValue}

value : bareword | nonBarewordValue

特别是,operatordirective中显示的operation规则应突出显示为关键字而非其正常角色。此外,应该可以突出显示=中的alias=中的operation不同。

一直在这个圈子里跑,并决定是时候问一个更熟悉黑暗艺术的人了。提前谢谢。

1 个答案:

答案 0 :(得分:0)

只是为了让您了解如何区分这些操作/别名关键字:

syn match alias /=\s*\w\+/ contains=alEqual
syn match operation /=\s*\w\+\ze\s\+\S\+/ contains=opEqual

syn match alEqual /=/ contained
syn match opEqual /=/ contained