From e61cfedf12b7c37d9eb41939705c71b2f5d9101e Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Wed, 29 Apr 2020 22:52:06 +0200 Subject: [PATCH] Scheme: Improved lambda parameter --- components/prism-racket.js | 9 ++++++++- components/prism-racket.min.js | 2 +- components/prism-scheme.js | 15 +++++++++++---- components/prism-scheme.min.js | 2 +- .../scheme/lambda_parameter_feature.test | 17 +++++++++++++++-- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/components/prism-racket.js b/components/prism-racket.js index 3face9d42e..f5eaac2b35 100644 --- a/components/prism-racket.js +++ b/components/prism-racket.js @@ -1,4 +1,11 @@ -Prism.languages.racket = Prism.languages.extend('scheme', {}); +Prism.languages.racket = Prism.languages.extend('scheme', { + 'lambda-parameter': { + // the racket lambda syntax is a lot more complex, so we won't even attempt to capture it. + // this will just prevent false positives of the `function` pattern + pattern: /(\(lambda\s+\()[^()'\s]+/, + lookbehind: true + } +}); // Add brackets to racket // The basic idea here is to go through all pattens of Scheme and replace all occurrences of "(" with the union of "(" diff --git a/components/prism-racket.min.js b/components/prism-racket.min.js index 50b08c19dd..5a3e25e8ca 100644 --- a/components/prism-racket.min.js +++ b/components/prism-racket.min.js @@ -1 +1 @@ -Prism.languages.racket=Prism.languages.extend("scheme",{}),Prism.languages.DFS(Prism.languages.racket,function(e,r){if("RegExp"===Prism.util.type(r)){var a=r.source.replace(/\\(.)|\[\^?((?:\\.|[^\\\]])*)\]/g,function(e,r,a){if(r){if("("===r)return"[([]";if(")"===r)return"[)\\]]"}return a?("^"===e[1]?"[^":"[")+a.replace(/\\(.)|[()]/g,function(e,r){return"("===e||"("===r?"([":")"===e||")"===r?")\\]":e})+"]":e});this[e]=RegExp(a,r.flags)}}),Prism.languages.insertBefore("racket","string",{lang:{pattern:/^#lang.+/m,greedy:!0,alias:"keyword"}}),Prism.languages.rkt=Prism.languages.racket; \ No newline at end of file +Prism.languages.racket=Prism.languages.extend("scheme",{"lambda-parameter":{pattern:/(\(lambda\s+\()[^()'\s]+/,lookbehind:!0}}),Prism.languages.DFS(Prism.languages.racket,function(e,a){if("RegExp"===Prism.util.type(a)){var r=a.source.replace(/\\(.)|\[\^?((?:\\.|[^\\\]])*)\]/g,function(e,a,r){if(a){if("("===a)return"[([]";if(")"===a)return"[)\\]]"}return r?("^"===e[1]?"[^":"[")+r.replace(/\\(.)|[()]/g,function(e,a){return"("===e||"("===a?"([":")"===e||")"===a?")\\]":e})+"]":e});this[e]=RegExp(r,a.flags)}}),Prism.languages.insertBefore("racket","string",{lang:{pattern:/^#lang.+/m,greedy:!0,alias:"keyword"}}),Prism.languages.rkt=Prism.languages.racket; \ No newline at end of file diff --git a/components/prism-scheme.js b/components/prism-scheme.js index f284a5d77a..e36aa1ee7f 100644 --- a/components/prism-scheme.js +++ b/components/prism-scheme.js @@ -13,10 +13,17 @@ Prism.languages.scheme = { greedy: true, alias: 'string' }, - 'lambda-parameter': { - pattern: /(\(lambda\s+\()[^()'\s]+/, - lookbehind: true - }, + 'lambda-parameter': [ + // https://www.cs.cmu.edu/Groups/AI/html/r4rs/r4rs_6.html#SEC30 + { + pattern: /(\(lambda\s+)[^()'\s]+/, + lookbehind: true + }, + { + pattern: /(\(lambda\s+\()[^()']+/, + lookbehind: true + } + ], 'keyword': { pattern: /(\()(?:define(?:-library|-macro|-syntax|-values)?|defmacro|(?:case-)?lambda|let(?:(?:\*|rec)?(?:-values)?|-syntax|rec-syntax)|else|if|cond|begin|delay(?:-force)?|parameterize|guard|set!|(?:quasi-)?quote|syntax-(?:case|rules))(?=[()\s]|$)/, lookbehind: true diff --git a/components/prism-scheme.min.js b/components/prism-scheme.min.js index 99aaccbe16..bdd367f019 100644 --- a/components/prism-scheme.min.js +++ b/components/prism-scheme.min.js @@ -1 +1 @@ -Prism.languages.scheme={comment:/;.*/,string:{pattern:/"(?:[^"\\]|\\.)*"/,greedy:!0},symbol:{pattern:/'[^()#'\s]+/,greedy:!0},character:{pattern:/#\\(?:[ux][a-fA-F\d]+|[-a-zA-Z]+|\S)/,greedy:!0,alias:"string"},"lambda-parameter":{pattern:/(\(lambda\s+\()[^()'\s]+/,lookbehind:!0},keyword:{pattern:/(\()(?:define(?:-library|-macro|-syntax|-values)?|defmacro|(?:case-)?lambda|let(?:(?:\*|rec)?(?:-values)?|-syntax|rec-syntax)|else|if|cond|begin|delay(?:-force)?|parameterize|guard|set!|(?:quasi-)?quote|syntax-(?:case|rules))(?=[()\s]|$)/,lookbehind:!0},builtin:{pattern:/(\()(?:(?:cons|car|cdr|list|call-with-current-continuation|call\/cc|append|abs|apply|eval)\b|null\?|pair\?|boolean\?|eof-object\?|char\?|procedure\?|number\?|port\?|string\?|vector\?|symbol\?|bytevector\?)(?=[()\s]|$)/,lookbehind:!0},number:{pattern:/([\s()])(?:(?:#d(?:#[ei])?|#[ei](?:#d)?)?[+-]?(?:(?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)(?:[+-](?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)i)?|(?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)i)|(?:#[box](?:#[ei])?|#[ei](?:#[box])?)[+-]?(?:[\da-fA-F]+(?:\/[\da-fA-F]+)?(?:[+-][\da-fA-F]+(?:\/[\da-fA-F]+)?i)?|[\da-fA-F]+(?:\/[\da-fA-F]+)?i))(?=[()\s]|$)/,lookbehind:!0},boolean:/#[ft](?=[()\s]|$)/,operator:{pattern:/(\()(?:[-+*%\/]|[<>]=?|=>?)(?=[()\s]|$)/,lookbehind:!0},function:{pattern:/(\()[^()'\s]+(?=[()\s]|$)/,lookbehind:!0},punctuation:/[()']/}; \ No newline at end of file +Prism.languages.scheme={comment:/;.*/,string:{pattern:/"(?:[^"\\]|\\.)*"/,greedy:!0},symbol:{pattern:/'[^()#'\s]+/,greedy:!0},character:{pattern:/#\\(?:[ux][a-fA-F\d]+|[-a-zA-Z]+|\S)/,greedy:!0,alias:"string"},"lambda-parameter":[{pattern:/(\(lambda\s+)[^()'\s]+/,lookbehind:!0},{pattern:/(\(lambda\s+\()[^()']+/,lookbehind:!0}],keyword:{pattern:/(\()(?:define(?:-library|-macro|-syntax|-values)?|defmacro|(?:case-)?lambda|let(?:(?:\*|rec)?(?:-values)?|-syntax|rec-syntax)|else|if|cond|begin|delay(?:-force)?|parameterize|guard|set!|(?:quasi-)?quote|syntax-(?:case|rules))(?=[()\s]|$)/,lookbehind:!0},builtin:{pattern:/(\()(?:(?:cons|car|cdr|list|call-with-current-continuation|call\/cc|append|abs|apply|eval)\b|null\?|pair\?|boolean\?|eof-object\?|char\?|procedure\?|number\?|port\?|string\?|vector\?|symbol\?|bytevector\?)(?=[()\s]|$)/,lookbehind:!0},number:{pattern:/([\s()])(?:(?:#d(?:#[ei])?|#[ei](?:#d)?)?[+-]?(?:(?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)(?:[+-](?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)i)?|(?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)i)|(?:#[box](?:#[ei])?|#[ei](?:#[box])?)[+-]?(?:[\da-fA-F]+(?:\/[\da-fA-F]+)?(?:[+-][\da-fA-F]+(?:\/[\da-fA-F]+)?i)?|[\da-fA-F]+(?:\/[\da-fA-F]+)?i))(?=[()\s]|$)/,lookbehind:!0},boolean:/#[ft](?=[()\s]|$)/,operator:{pattern:/(\()(?:[-+*%\/]|[<>]=?|=>?)(?=[()\s]|$)/,lookbehind:!0},function:{pattern:/(\()[^()'\s]+(?=[()\s]|$)/,lookbehind:!0},punctuation:/[()']/}; \ No newline at end of file diff --git a/tests/languages/scheme/lambda_parameter_feature.test b/tests/languages/scheme/lambda_parameter_feature.test index afc03ab76d..a076879750 100644 --- a/tests/languages/scheme/lambda_parameter_feature.test +++ b/tests/languages/scheme/lambda_parameter_feature.test @@ -1,13 +1,26 @@ +(lambda x x) +(lambda (x) x) (lambda (foo bar) (concat foo bar)) ---------------------------------------------------- [ + ["punctuation", "("], + ["keyword", "lambda"], + ["lambda-parameter", "x"], + " x", + ["punctuation", ")"], + ["punctuation", "("], + ["keyword", "lambda"], + ["punctuation", "("], + ["lambda-parameter", "x"], + ["punctuation", ")"], + " x", + ["punctuation", ")"], ["punctuation", "("], ["keyword", "lambda"], ["punctuation", "("], - ["lambda-parameter", "foo"], - " bar", + ["lambda-parameter", "foo bar"], ["punctuation", ")"], ["punctuation", "("], ["function", "concat"],