I’m trying to build a syntax-highlighter for Cottle (which is the script language used in EDDI, a text-to-speech companion program for Elite:Dangerous).
Here’s a working script:
{_ this is a working cottle script for EDDI }
{_ this
is
a
comment
}
{set variable to 1} {_ this is a comment}
unquoted string with 1 {variable} inside. {_ this is a comment}
'single-quoted string with 1 {variable} inside.' {_ this is a comment}
"double-quoted string with 1 {variable} inside." {_ this is a comment}
{if false:
unquoted string with 1 {variable} inside. {_ this is a comment}
'single-quoted string with 1 {variable} inside.' {_ this is a comment}
"double-quoted string with 1 {variable} inside." {_ this is a comment}
|elif:
unquoted string with 1 {variable} inside. {_ this is a comment}
'single-quoted string with 1 {variable} inside.' {_ this is a comment}
"double-quoted string with 1 {variable} inside." {_ this is a comment}
|else:
unquoted string with a {Emphasize("this is a string_parameter")} inside.
"double-quoted string with a {Emphasize("this is a string_parameter")} inside."
'single-quoted string with a {Emphasize("this is a {cat("string", "_", "parameter")} ")} inside'.
'single-quoted string with a {Emphasize("this is a { {_ this is a comment}
{_ this is a comment}
cat("string", "_", "parameter")
} ")} inside.'
}
'It\'s a "single-quoted string" with 1 {variable} inside.' {_ this is a comment}
"It's a \"double-quoted string\" with 1 {variable} inside." {_ this is a comment}
It\'s a \"unquoted string\" with 1 {variable} inside. {_ this is a comment}
In this script, everything that’s not {code
is treated as a string (then spoken by EDDI), even if unquoted. BUT: you can open a code block inside a string, to use variables, call functions or even call other scripts… and these code blocks can contains other strings.
This kind of nesting makes me crazy, but i think i’ve sort almost all.
(Here you may look at the syntax-highlight used by EDDI editor:
https://github.com/EDCD/EDDI/blob/develop/SpeechResponder/Cottle.xshd
It is very well commented and explains how it works better than my poor english).
To explain my problem, here i will just highlight with different color comments, code and strings.
So this is my “simplified” sublime-syntax file:
%YAML 1.2
---
file_extensions:
- cottle
scope: source.cottle
contexts:
main:
- include: StringOrCode
StringOrCode:
- include: comment
- include: cottle_code_block
- include: string
comment:
- match: \{\_
scope: punctuation.definition.comment.cottle
push: comment_body
comment_body:
- meta_scope: comment.block.cottle
- match: \}
scope: punctuation.definition.comment.cottle
pop: true
cottle_code_block:
- match: \{
scope: punctuation.section.block.begin.cottle
push: cottle_code_block_body
cottle_code_block_body:
- meta_scope: meta.block.cottle keyword.other.cottle
- match: \}
scope: punctuation.section.block.end.cottle
pop: true
- include: StringOrCode
cottle_code_block_body_within_string:
- clear_scopes: 1
- include: cottle_code_block_body
string:
- include: escape_characters
- include: double_quoted_string
- include: single_quoted_string
escape_characters:
- match: \\.
scope: constant.character.escape
double_quoted_string:
- match: \"
scope: punctuation.definition.string.begin.cottle
push: double_quoted_string_body
double_quoted_string_body:
- meta_scope: meta.string.cottle string.quoted.double.cottle
- include: escape_characters
- match: \"
scope: punctuation.definition.string.end.cottle
pop: true
- include: Nested_Code_In_String
single_quoted_string:
- match: \'
scope: punctuation.definition.string.begin.cottle
push: single_quoted_string_body
single_quoted_string_body:
- meta_scope: meta.string.cottle string.quoted.single.cottle
- include: escape_characters
- match: \'
scope: punctuation.definition.string.end.cottle
pop: true
- include: Nested_Code_In_String
Nested_Code_In_String:
- match: \{
scope: punctuation.section.block.begin.cottle
push: cottle_code_block_body_within_string
# - include: StringOrCode
And this is how it looks:
(“code” is blue, “string” is brown, “comment” is grey, “source” is red)
My big, first problem is: how to catch unquoted strings???
I don’t know how should i manage them inside the syntax file.
Also, i don’t know how to solve the nesting between strings and code: if you look at line 11, the code inside the string is not highlighted as code, and i don’t know why.
Could someone give me some help?