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

C: Added char token #3207

Merged
merged 2 commits into from Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/prism-arduino.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/prism-arduino.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion components/prism-c.js
Expand Up @@ -3,6 +3,11 @@ Prism.languages.c = Prism.languages.extend('clike', {
pattern: /\/\/(?:[^\r\n\\]|\\(?:\r\n?|\n|(?![\r\n])))*|\/\*[\s\S]*?(?:\*\/|$)/,
greedy: true
},
'string': {
// https://en.cppreference.com/w/c/language/string_literal
pattern: /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"/,
greedy: true
},
'class-name': {
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+|\b[a-z]\w*_t\b/,
lookbehind: true
Expand All @@ -13,6 +18,14 @@ Prism.languages.c = Prism.languages.extend('clike', {
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/
});

Prism.languages.insertBefore('c', 'string', {
'char': {
// https://en.cppreference.com/w/c/language/character_constant
pattern: /'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n]){0,32}'/,
greedy: true
}
});

Prism.languages.insertBefore('c', 'string', {
'macro': {
// allow for multiline macro definitions
Expand All @@ -30,6 +43,7 @@ Prism.languages.insertBefore('c', 'string', {
},
Prism.languages.c['string']
],
'char': Prism.languages.c['char'],
'comment': Prism.languages.c['comment'],
'macro-name': [
{
Expand All @@ -55,7 +69,10 @@ Prism.languages.insertBefore('c', 'string', {
inside: Prism.languages.c
}
}
},
}
});

Prism.languages.insertBefore('c', 'function', {
// highlight predefined macros as constants
'constant': /\b(?:EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|__DATE__|__FILE__|__LINE__|__TIMESTAMP__|__TIME__|__func__|stderr|stdin|stdout)\b/
});
Expand Down
2 changes: 1 addition & 1 deletion components/prism-c.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/languages/bison/char_feature.test
@@ -0,0 +1,9 @@
'+'
'\''

----------------------------------------------------

[
["char", "'+'"],
["char", "'\\''"]
]
21 changes: 14 additions & 7 deletions tests/languages/bison/string_feature.test
@@ -1,21 +1,28 @@
%%
foo: 'foo' "foo";
bar: '\'' "\"";
foo: "foo";
bar: "\"";
%%

----------------------------------------------------

[
["bison", [
["punctuation", "%%"],
["property", "foo"], ["punctuation", ":"],
["string", "'foo'"], ["string", "\"foo\""], ["punctuation", ";"],
["property", "bar"], ["punctuation", ":"],
["string", "'\\''"], ["string", "\"\\\"\""], ["punctuation", ";"],

["property", "foo"],
["punctuation", ":"],
["string", "\"foo\""],
["punctuation", ";"],

["property", "bar"],
["punctuation", ":"],
["string", "\"\\\"\""],
["punctuation", ";"],

["punctuation", "%%"]
]]
]

----------------------------------------------------

Checks for strings.
Checks for strings.
35 changes: 35 additions & 0 deletions tests/languages/c/char_feature.test
@@ -0,0 +1,35 @@
'a'
'\n'
'\13'
'🍌'
'ab'

u'a'
u'¢'
u'猫'
U'猫'
L'猫'

'\1\2\3\4'
'\xFF'
u'\U0001f34c'

----------------------------------------------------

[
["char", "'a'"],
["char", "'\\n'"],
["char", "'\\13'"],
["char", "'🍌'"],
["char", "'ab'"],

"\r\n\r\nu", ["char", "'a'"],
"\r\nu", ["char", "'¢'"],
"\r\nu", ["char", "'猫'"],
"\r\nU", ["char", "'猫'"],
"\r\nL", ["char", "'猫'"],

["char", "'\\1\\2\\3\\4'"],
["char", "'\\xFF'"],
"\r\nu", ["char", "'\\U0001f34c'"]
]
34 changes: 34 additions & 0 deletions tests/languages/c/string_feature.test
@@ -0,0 +1,34 @@
""
"foo"
"\x12"
"3"

"\xff""f"

"foo\
bar"

"a猫🍌"
u8"a猫🍌"
u"a猫🍌"
U"a猫🍌"
L"a猫🍌"

----------------------------------------------------

[
["string", "\"\""],
["string", "\"foo\""],
["string", "\"\\x12\""],
["string", "\"3\""],

["string", "\"\\xff\""], ["string", "\"f\""],

["string", "\"foo\\\r\nbar\""],

["string", "\"a猫🍌\""],
"\r\nu8", ["string", "\"a猫🍌\""],
"\r\nu", ["string", "\"a猫🍌\""],
"\r\nU", ["string", "\"a猫🍌\""],
"\r\nL", ["string", "\"a猫🍌\""]
]
12 changes: 12 additions & 0 deletions tests/languages/objectivec/char_feature.test
@@ -0,0 +1,12 @@
''
'fo\'o'
'foo\
bar'

----------------------------------------------------

[
["char", "''"],
["char", "'fo\\'o'"],
["char", "'foo\\\r\nbar'"]
]
11 changes: 1 addition & 10 deletions tests/languages/objectivec/string_feature.test
Expand Up @@ -3,11 +3,6 @@
"foo\
bar"

''
'fo\'o'
'foo\
bar'

@""
@"fo\"o"
@"foo\
Expand All @@ -20,15 +15,11 @@ bar"
["string", "\"fo\\\"o\""],
["string", "\"foo\\\r\nbar\""],

["string", "''"],
["string", "'fo\\'o'"],
["string", "'foo\\\r\nbar'"],

["string", "@\"\""],
["string", "@\"fo\\\"o\""],
["string", "@\"foo\\\r\nbar\""]
]

----------------------------------------------------

Checks for strings.
Checks for strings.