From f1e850fdb465bcb32ae728c57fca855d705e3178 Mon Sep 17 00:00:00 2001 From: Steven Van Impe Date: Sun, 27 Dec 2020 13:25:36 +0100 Subject: [PATCH 1/2] (swift) Support operator and precedencegroup declarations. --- CHANGES.md | 5 +++ src/languages/lib/kws_swift.js | 15 +++++-- src/languages/swift.js | 45 +++++++++++++++++++ .../swift/operator-declarations.expect.txt | 3 ++ test/markup/swift/operator-declarations.txt | 3 ++ test/markup/swift/precedencegroup.expect.txt | 8 ++++ test/markup/swift/precedencegroup.txt | 8 ++++ 7 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 test/markup/swift/operator-declarations.expect.txt create mode 100644 test/markup/swift/operator-declarations.txt create mode 100644 test/markup/swift/precedencegroup.expect.txt create mode 100644 test/markup/swift/precedencegroup.txt diff --git a/CHANGES.md b/CHANGES.md index ceb04d4ac3..d01ff1c831 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,12 @@ New Languages: - Added 3rd party Laravel Blade grammar to SUPPORTED_LANGUAGES (#2944) [Michael Newton][] +Language grammar improvements: + +- enh(swift) Improved highlighting for operator and precedencegroup declarations. (#2938) [Steven Van Impe][] + [Michael Newton]: https://github.com/miken32 +[Steven Van Impe]: https://github.com/svanimpe/ ## Version 10.5.0 diff --git a/src/languages/lib/kws_swift.js b/src/languages/lib/kws_swift.js index a52ca459bc..b349c52603 100644 --- a/src/languages/lib/kws_swift.js +++ b/src/languages/lib/kws_swift.js @@ -117,10 +117,6 @@ export const keywords = [ // NOTE: Contextual keywords are reserved only in specific contexts. // Ideally, these should be matched using modes to avoid false positives. -// TODO: Create a PRECEDENCE_GROUP mode to match the remaining contextual keywords: -// assignment associativity higherThan left lowerThan none right -// These aren't included in the list because they result in mostly false positives. - // Literals. export const literals = [ 'false', @@ -128,6 +124,17 @@ export const literals = [ 'true' ]; +// Keywords used in precedence groups. +export const precedencegroupKeywords = [ + 'assignment', + 'associativity', + 'higherThan', + 'left', + 'lowerThan', + 'none', + 'right' +]; + // Keywords that start with a number sign (#). // #available is handled separately. export const numberSignKeywords = [ diff --git a/src/languages/swift.js b/src/languages/swift.js index 5f8e1a7389..aa3a3fce37 100644 --- a/src/languages/swift.js +++ b/src/languages/swift.js @@ -413,6 +413,49 @@ export default function(hljs) { ], illegal: /\[|%/ }; + // https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID380 + const OPERATOR_DECLARATION = { + beginKeywords: 'operator', + contains: [ + { + match: /\s+/, + relevance: 0 + }, + { + className: 'title', + match: Swift.operator, + endsParent: true, + relevance: 0 + } + ] + }; + + // https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID550 + const PRECEDENCEGROUP = { + beginKeywords: 'precedencegroup', + contains: [ + { + match: /\s+/, + relevance: 0 + }, + { + className: 'title', + match: Swift.typeIdentifier, + relevance: 0 + }, + { + begin: /{/, + end: /}/, + relevance: 0, + endsParent: true, + keywords: [ + ...Swift.precedencegroupKeywords, + ...Swift.literals + ].join(' '), + contains: [ TYPE ] + } + ] + }; // Add supported submodes to string interpolation. for (const variant of STRING.variants) { @@ -460,6 +503,8 @@ export default function(hljs) { ...KEYWORD_MODES ] }, + OPERATOR_DECLARATION, + PRECEDENCEGROUP, { beginKeywords: 'import', end: /$/, diff --git a/test/markup/swift/operator-declarations.expect.txt b/test/markup/swift/operator-declarations.expect.txt new file mode 100644 index 0000000000..3330ebfcbb --- /dev/null +++ b/test/markup/swift/operator-declarations.expect.txt @@ -0,0 +1,3 @@ +prefix operator +++ +postfix operator +++ +infix operator +-: AdditionPrecedence diff --git a/test/markup/swift/operator-declarations.txt b/test/markup/swift/operator-declarations.txt new file mode 100644 index 0000000000..695156346a --- /dev/null +++ b/test/markup/swift/operator-declarations.txt @@ -0,0 +1,3 @@ +prefix operator +++ +postfix operator +++ +infix operator +-: AdditionPrecedence diff --git a/test/markup/swift/precedencegroup.expect.txt b/test/markup/swift/precedencegroup.expect.txt new file mode 100644 index 0000000000..4cb8ebe3f1 --- /dev/null +++ b/test/markup/swift/precedencegroup.expect.txt @@ -0,0 +1,8 @@ +precedencegroup MyGroup { + higherThan: OtherGroup, AnotherGroup + lowerThan: OtherGroup, AnotherGroup + assignment: true + associativity: left + associativity: right + associativity: none +} diff --git a/test/markup/swift/precedencegroup.txt b/test/markup/swift/precedencegroup.txt new file mode 100644 index 0000000000..fdadf9889b --- /dev/null +++ b/test/markup/swift/precedencegroup.txt @@ -0,0 +1,8 @@ +precedencegroup MyGroup { + higherThan: OtherGroup, AnotherGroup + lowerThan: OtherGroup, AnotherGroup + assignment: true + associativity: left + associativity: right + associativity: none +} From ccb8a453f4815ede0c19a72a61e7f58c7fa0d6e9 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 28 Dec 2020 11:55:24 -0500 Subject: [PATCH 2/2] add modes.MATCH_NOTHING_RE --- CHANGES.md | 7 +++++++ src/languages/swift.js | 10 ++-------- src/lib/modes.js | 1 + types/index.d.ts | 1 + 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d01ff1c831..3dfc86202f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,8 +8,15 @@ Language grammar improvements: - enh(swift) Improved highlighting for operator and precedencegroup declarations. (#2938) [Steven Van Impe][] +Parser: + +- add `modes.MATCH_NOTHING_RE` that will never match + - This can be used with `end` to hold a mode open (it must then be ended with + `endsParent` in one of it's children modes) [Josh Goebel][] + [Michael Newton]: https://github.com/miken32 [Steven Van Impe]: https://github.com/svanimpe/ +[Josh Goebel]: https://github.com/joshgoebel ## Version 10.5.0 diff --git a/src/languages/swift.js b/src/languages/swift.js index aa3a3fce37..f499d1a9a7 100644 --- a/src/languages/swift.js +++ b/src/languages/swift.js @@ -416,11 +416,8 @@ export default function(hljs) { // https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID380 const OPERATOR_DECLARATION = { beginKeywords: 'operator', + end: hljs.MATCH_NOTHING_RE, contains: [ - { - match: /\s+/, - relevance: 0 - }, { className: 'title', match: Swift.operator, @@ -433,11 +430,8 @@ export default function(hljs) { // https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID550 const PRECEDENCEGROUP = { beginKeywords: 'precedencegroup', + end: hljs.MATCH_NOTHING_RE, contains: [ - { - match: /\s+/, - relevance: 0 - }, { className: 'title', match: Swift.typeIdentifier, diff --git a/src/lib/modes.js b/src/lib/modes.js index eb4c7f137d..e4d2ab45b9 100644 --- a/src/lib/modes.js +++ b/src/lib/modes.js @@ -2,6 +2,7 @@ import { inherit } from './utils.js'; import * as regex from './regex.js'; // Common regexps +export const MATCH_NOTHING_RE = /\b\B/; export const IDENT_RE = '[a-zA-Z]\\w*'; export const UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*'; export const NUMBER_RE = '\\b\\d+(\\.\\d+)?'; diff --git a/types/index.d.ts b/types/index.d.ts index e572abae87..223aa3e0c1 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -58,6 +58,7 @@ interface ModesAPI { // built in regex IDENT_RE: string UNDERSCORE_IDENT_RE: string + MATCH_NOTHING_RE: string NUMBER_RE: string C_NUMBER_RE: string BINARY_NUMBER_RE: string