Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further syntax highlighting improvements #220

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 97 additions & 11 deletions Manual/contents/assets/scripts/gml.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ export default function(hljs) {
"iap_storeload_failed",
"iap_storeload_ok",
"iap_unavailable",
"infinity",
"input_type",
"kbv_autocapitalize_characters",
"kbv_autocapitalize_none",
Expand Down Expand Up @@ -964,24 +965,88 @@ export default function(hljs) {
* supported in the engine.)
*/
const DOT_ACCESSOR_REG = /\b\.\b/;

/**
* Expressions, which form part of a valid statement.
*/
const EXPRESSION = [];

/**
* A template string substitution. `contains` is filled in after `EXPRESSION` is defined due to
* nesting.
*/
const STRING_SUBSTITUTION = {
begin: /{/,
end: /}/,
beginScope: "literal",
endScope: "literal",
contains: EXPRESSION
};

/**
* A template string substitution for use with the older `string()` optional args with `"{0}"`,
* etc.
*/
const STRING_NUMERICAL_SUBSTITUTION = {
match: /{[0-9]+}/,
scope: "literal"
};

/**
* An escape sequence in a string.
*/
const STRING_ESCAPE = {
scope: "literal",
variants: [
{ match: /\\u[a-fA-F0-9]{1,6}/ },
{ match: /\\[^\n]/ }
]
};

/**
* Various types of strings supported in the engine.
*/
const STRING = {
variants: [
hljs.QUOTE_STRING_MODE,
{
begin: /\$"/,
end: "\"",
beginScope: "string",
endScope: "string",
contains: [
STRING_ESCAPE,
STRING_SUBSTITUTION,
{
match: /[^\n"{]/,
scope: "string"
}
]
},
{
scope: "string",
begin: "@'",
end: "'"
end: "'",
contains: [STRING_NUMERICAL_SUBSTITUTION]
},
{
scope: "string",
begin: "@\"",
end: "\""
end: "\"",
contains: [STRING_NUMERICAL_SUBSTITUTION]
},
{
scope: "string",
begin: /"/,
end: /"/,
illegal: "\\n",
contains: [
STRING_ESCAPE,
STRING_NUMERICAL_SUBSTITUTION
]
}
]
};

/**
* Various representations of numbers
*/
Expand All @@ -995,6 +1060,7 @@ export default function(hljs) {
{ match: /\b[0-9][0-9_.]*/ }
]
};

/**
* Pre-processor modes for macro definitions and regions.
*/
Expand Down Expand Up @@ -1035,10 +1101,12 @@ export default function(hljs) {
},
]
};

/**
* A single-line comment.
*/
const COMMENT_LINE = hljs.COMMENT('//', /\$|\n/);

/**
* Modes for the types of comments supported in GML.
*/
Expand All @@ -1048,6 +1116,7 @@ export default function(hljs) {
hljs.C_BLOCK_COMMENT_MODE,
]
};

/**
* Dot accessor usage with a special highlighting case for `global`.
*/
Expand Down Expand Up @@ -1083,6 +1152,7 @@ export default function(hljs) {
}
},
];

/**
* Function call sites, just looking for `<ident>(`. This creates false positives
* for keywords such as `if (<condition>)`, so has lower priority in the mode `contains` list.
Expand All @@ -1097,6 +1167,7 @@ export default function(hljs) {
1: "function"
}
};

/**
* The manual likes using `obj_` and such to define assets. Sneaky trick to make it look nicer :P
*/
Expand All @@ -1108,16 +1179,17 @@ export default function(hljs) {
{ begin: "obj_" },
]
};

/**
* Expressions, which form part of a valid statement.
* A ternary expression, matching partial ternary as `? <EXPRESSION> :`.
* Effectively exists to prevent {@link STRUCT_LITERAL_MEMBER} from stealing `<EXPRESSION> :`.
*/
const EXPRESSION = [
STRING,
PROP_ACCESS,
NUMBER,
FUNCTION_CALL,
USER_ASSET_CONSTANT
];
const TERNARY = {
begin: /\?/,
end: /:/,
contains: EXPRESSION
};

const SWITCH_CASE = {
begin: [
/case/,
Expand All @@ -1129,6 +1201,7 @@ export default function(hljs) {
},
contains: EXPRESSION
};

/**
* A struct variable declaration, of `<ident>:`
*/
Expand All @@ -1142,6 +1215,7 @@ export default function(hljs) {
2: "variable-instance"
},
};

/**
* A function declaration matching for:
* ```gml
Expand All @@ -1160,6 +1234,7 @@ export default function(hljs) {
3: "function"
}
};

/**
* An enum definition in the form:
* ```gml
Expand Down Expand Up @@ -1199,6 +1274,16 @@ export default function(hljs) {
}
]
};

EXPRESSION.push(
STRING,
TERNARY,
PROP_ACCESS,
NUMBER,
FUNCTION_CALL,
USER_ASSET_CONSTANT
);

return {
name: 'GML',
case_insensitive: false, // language is case-sensitive
Expand All @@ -1218,6 +1303,7 @@ export default function(hljs) {
// Prevent keywords being taken by function calls.
beginKeywords: KEYWORDS.join(" ")
},
TERNARY,
STRUCT_LITERAL_MEMBER,
FUNCTION_DECLARATION,
FUNCTION_CALL,
Expand Down
Loading