From 952fa0a15262518047a40dea60642497612a2695 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sat, 8 May 2021 08:40:01 -0400 Subject: [PATCH] (enh) simplify Python grammar --- src/languages/python.js | 41 ++++++++++++------- test/markup/python-repl/sample.expect.txt | 2 +- test/markup/python/decorators.expect.txt | 16 ++++---- .../function-header-comments.expect.txt | 8 ++-- test/markup/python/function-header.expect.txt | 2 +- test/markup/python/keywords.expect.txt | 4 +- .../python/matrix-multiplication.expect.txt | 4 +- 7 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/languages/python.js b/src/languages/python.js index ea765f4651..5ea0dde6bc 100644 --- a/src/languages/python.js +++ b/src/languages/python.js @@ -5,6 +5,7 @@ Website: https://www.python.org Category: common */ +import { IDENT_RE } from '../lib/modes.js'; import * as regex from '../lib/regex.js'; export default function(hljs) { @@ -375,28 +376,38 @@ export default function(hljs) { STRING, COMMENT_TYPE, hljs.HASH_COMMENT_MODE, + { + match: [ + /def/, /\s+/, + IDENT_RE + ], + scope: { + 1: "keyword", + 3: "title.function" + }, + contains: [ PARAMS ] + }, { variants: [ { - className: 'function', - beginKeywords: 'def' + match: [ + /class/, /\s+/, + IDENT_RE, /\s*/, + /\(\s*/, IDENT_RE,/\s*\)/ + ], }, { - className: 'class', - beginKeywords: 'class' + match: [ + /class/, /\s+/, + IDENT_RE + ], } ], - end: /:/, - illegal: /[${=;\n,]/, - contains: [ - hljs.UNDERSCORE_TITLE_MODE, - PARAMS, - { - begin: /->/, - endsWithParent: true, - keywords: KEYWORDS - } - ] + scope: { + 1: "keyword", + 3: "title.class", + 6: "title.class.inherited", + } }, { className: 'meta', diff --git a/test/markup/python-repl/sample.expect.txt b/test/markup/python-repl/sample.expect.txt index 1f3c7733eb..95dd4c78ad 100644 --- a/test/markup/python-repl/sample.expect.txt +++ b/test/markup/python-repl/sample.expect.txt @@ -11,7 +11,7 @@ foo = 42" >>> """ ... abc ... """ ->>> def test(): +>>> def test(): ... pass >>> >>> diff --git a/test/markup/python/decorators.expect.txt b/test/markup/python/decorators.expect.txt index a0ab79766f..174834925f 100644 --- a/test/markup/python/decorators.expect.txt +++ b/test/markup/python/decorators.expect.txt @@ -1,31 +1,31 @@ @foo -def bar(): +def bar(): pass @foo # bar -def baz(): +def baz(): pass @foo.bar.baz -def qux(): +def qux(): pass @surround_with("#", repeat=3) -def text(): +def text(): return "hi!" @py38.style -def func(): +def func(): pass @py["3.9"].style -def func(): +def func(): pass @py[3.9].style -def func(): +def func(): pass @2 + 2 == 5 -def func(): +def func(): pass diff --git a/test/markup/python/function-header-comments.expect.txt b/test/markup/python/function-header-comments.expect.txt index 061a1a4742..35041289d8 100644 --- a/test/markup/python/function-header-comments.expect.txt +++ b/test/markup/python/function-header-comments.expect.txt @@ -1,10 +1,10 @@ -def foo( +def foo( bar, # commment -): +): pass -class Foo(collections.namedtuple('Test'), ( +class Foo(collections.namedtuple('Test'), ( 'name', # comment -)): +)): pass diff --git a/test/markup/python/function-header.expect.txt b/test/markup/python/function-header.expect.txt index 197b91d7cd..327f2b6118 100644 --- a/test/markup/python/function-header.expect.txt +++ b/test/markup/python/function-header.expect.txt @@ -1,2 +1,2 @@ -def f(x: int, *, y: bool = True) -> None: +def f(x: int, *, y: bool = True) -> None: pass diff --git a/test/markup/python/keywords.expect.txt b/test/markup/python/keywords.expect.txt index 96aa34ebff..501f670464 100644 --- a/test/markup/python/keywords.expect.txt +++ b/test/markup/python/keywords.expect.txt @@ -1,5 +1,5 @@ -class Shorty(dict): - def len(self): +class Shorty(dict): + def len(self): return NotImplemented x = Shorty() diff --git a/test/markup/python/matrix-multiplication.expect.txt b/test/markup/python/matrix-multiplication.expect.txt index 256895b24e..91ad4b9261 100644 --- a/test/markup/python/matrix-multiplication.expect.txt +++ b/test/markup/python/matrix-multiplication.expect.txt @@ -1,7 +1,7 @@ @meta -class C: +class C: @decorator - def f(self, H, V, beta, r): + def f(self, H, V, beta, r): S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r) return S