简体   繁体   English

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

[英]How should I approach creating a Vim syntax for this simple toy language?

I have an extremely simple toy language for which I'd like to create a Vim syntax. 我有一种非常简单的玩具语言,我想为其创建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

The major problem I seem to be running into is that this language doesn't have a fixed set of keywords, but rather the token at a certain position per line is treated as a keyword. 我似乎遇到的主要问题是,该语言没有一组固定的关键字,但是每行某个位置的标记被视为关键字。 I'm not sure where to look for examples of this. 我不确定在哪里可以找到这样的例子。

Here is a fairly complete outline of the syntax: 这是一个相当完整的语法概述:

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

In particular, the operator rule as it appears in directive and operation should be highlighted as a keyword instead of its normal role. 特别是,在directiveoperation出现的operator规则应突出显示为关键字,而不是其常规角色。 Additionally, it should be possible to highlight the = in an alias differently from the = in an operation . 另外,它应该有可能要突出=在一个alias从不同=在一个operation

Have been running in circles on this one and decided it was time to ask someone more familiar with the dark arts. 一直在这个圈子里跑来跑去,决定是时候该问问一个更了解黑暗艺术的人了。 Thanks in advance. 提前致谢。

just to give you an idea how to distinguish between those operation/alias keywords: 只是为了让您了解如何区分这些操作/别名关键字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM