Как мне подойти к созданию синтаксиса Vim для этого простого игрушечного языка? - PullRequest
1 голос
/ 27 июня 2011

У меня очень простой игрушечный язык, для которого я хотел бы создать синтаксис 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

В частности, правило operator в том виде, в каком оно появляется в directive и operation, должно выделяться как ключевое слово, а не как обычная роль. Кроме того, должна быть возможность выделить = в alias иначе, чем = в operation.

В этом кругу бегал кругами и решил, что пришло время спросить кого-то, более знакомого с темными искусствами. Заранее спасибо.

1 Ответ

0 голосов
/ 27 августа 2011

просто, чтобы дать вам представление о том, как отличить эти ключевые слова операции / псевдонима:

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
...