From fc5c0cceea8682be51cf27a75dee9d662af75d4f Mon Sep 17 00:00:00 2001 From: Nikolay Antipov Date: Sun, 4 Jun 2023 21:12:11 +0200 Subject: [PATCH 1/5] add OpenSCAD lexer (#886) --- AUTHORS | 1 + pygments/lexers/_mapping.py | 1 + pygments/lexers/openscad.py | 67 +++++ tests/snippets/openscad/test_basic.txt | 370 +++++++++++++++++++++++++ 4 files changed, 439 insertions(+) create mode 100644 pygments/lexers/openscad.py create mode 100644 tests/snippets/openscad/test_basic.txt diff --git a/AUTHORS b/AUTHORS index 5607d65668..503af632cc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -267,5 +267,6 @@ Other contributors, listed alphabetically, are: * Marc Auberer -- Spice lexer * Amr Hesham -- Carbon lexer * diskdance -- Wikitext lexer +* Nikolay Antipov -- OpenSCAD lexer Many thanks for all contributions! diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 87d261c715..76d278382f 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -353,6 +353,7 @@ 'OocLexer': ('pygments.lexers.ooc', 'Ooc', ('ooc',), ('*.ooc',), ('text/x-ooc',)), 'OpaLexer': ('pygments.lexers.ml', 'Opa', ('opa',), ('*.opa',), ('text/x-opa',)), 'OpenEdgeLexer': ('pygments.lexers.business', 'OpenEdge ABL', ('openedge', 'abl', 'progress'), ('*.p', '*.cls'), ('text/x-openedge', 'application/x-openedge')), + 'OpenScadLexer': ('pygments.lexers.openscad', 'OpenSCAD', ('openscad',), ('*.scad',), ()), 'OutputLexer': ('pygments.lexers.special', 'Text output', ('output',), (), ()), 'PacmanConfLexer': ('pygments.lexers.configs', 'PacmanConf', ('pacmanconf',), ('pacman.conf',), ()), 'PanLexer': ('pygments.lexers.dsls', 'Pan', ('pan',), ('*.pan',), ()), diff --git a/pygments/lexers/openscad.py b/pygments/lexers/openscad.py new file mode 100644 index 0000000000..e1a508d47b --- /dev/null +++ b/pygments/lexers/openscad.py @@ -0,0 +1,67 @@ +""" + pygments.lexers.openscad + ~~~~~~~~~~~~~~~~~~~~~~~~ + + Lexers for the OpenSCAD languages. + + :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from pygments.lexer import RegexLexer, bygroups +from pygments.token import Text, Comment, Punctuation, Operator, Keyword, Name, Literal + +__all__ = ['OpenScadLexer'] + + +class OpenScadLexer(RegexLexer): + name = "OpenSCAD" + aliases = ["openscad"] + filenames = ["*.scad"] + + tokens = { + "root": [ + (r"[^\S\n]+", Text), + (r"\n", Text), + (r"//(\n|[\w\W]*?[^\\]\n)", Comment.Singleline), + (r"/(\\\n)?[*][\w\W]*?[*](\\\n)?/", Comment.Mutliline), + (r"/(\\\n)?[*][\w\W]*", Comment.Mutliline), + (r"[{}\[\]\(\),;:]", Punctuation), + (r"[*!#%\-+=?/]", Operator), + (r"<=|<|==|!=|>=|>|&&|\|\|", Operator), + (r"\$(f[asn]|t|vp[rtd]|children)", Operator), + (r"(undef|PI)\b", Keyword.Constant), + ( + r"(use|include)((?:\s|\\\\s)+)", + bygroups(Keyword.Namespace, Text), + "includes", + ), + ( + r"(module)(\s*)([^\s\(]+)", + bygroups(Keyword.Namespace, Text, Name.Namespace), + ), + ( + r"(function)(\s*)([^\s\(]+)", + bygroups(Keyword.Declaration, Text, Name.Function), + ), + (r"\b(true|false)\b", Literal), + ( + r"\b(function|module|include|use|for|intersection_for|if|else|return)\b", + Keyword, + ), + ( + r"\b(circle|square|polygon|text|sphere|cube|cylinder|polyhedron|translate|rotate|scale|resize|mirror|multmatrix|color|offset|hull|minkowski|union|difference|intersection|abs|sign|sin|cos|tan|acos|asin|atan|atan2|floor|round|ceil|ln|log|pow|sqrt|exp|rands|min|max|concat|lookup|str|chr|search|version|version_num|norm|cross|parent_module|echo|import|import_dxf|dxf_linear_extrude|linear_extrude|rotate_extrude|surface|projection|render|dxf_cross|dxf_dim|let|assign|len)\b", + Name.Builtin, + ), + (r"\bchildren\b", Name.Builtin.Pseudo), + (r""(\\\\|\\"|[^"])*"", Literal.StringDouble), + (r"-?\d+(\.\d+)?(e[+-]?\d+)?", Literal.Number), + (r"[a-zA-Z_]\w*", Name), + ], + "includes": [ + ( + r"(<)([^>]*)(>)", + bygroups(Punctuation, Comment.PreprocFile, Punctuation), + ), + ], + } diff --git a/tests/snippets/openscad/test_basic.txt b/tests/snippets/openscad/test_basic.txt new file mode 100644 index 0000000000..9a6c37ac71 --- /dev/null +++ b/tests/snippets/openscad/test_basic.txt @@ -0,0 +1,370 @@ +---input--- + +/* + Multiline comment +*/ + +egg(1500); + +// Single line comment + +module egg(length, start=-1000, end=-1000) { + rotate_extrude() + translate([0, -length/2, 0]) + rotate([0, 0, 90]) + polygon(egg_half_poly( + length, + start == -1000 ? 0 : start, + end == -1000 ? length : end + )); +} + +function egg_half_poly(length, start, end) = + concat( + [[start, 0]], + [for (x=[start : (release ? 0.5 : 1) : end]) [x, egg_eq(x - length/2, length)]], + [[end, 0]] + ); + +function egg_eq(x, length) = + length / 1.25 / 2 * sqrt( + (length * length - 4 * x * x) / + (length * length + 8 * egg_w * x + 4 * egg_w * egg_w) + ); + + +---tokens--- +'/*\n Multiline comment\n*/' Comment.Mutliline +'\n' Text + +'\n' Text + +'egg' Name +'(' Punctuation +'1500' Literal.Number +')' Punctuation +';' Punctuation +'\n' Text + +'\n' Text + +'// Single line comment\n' Comment.Singleline + +'\n' Text + +'module' Keyword.Namespace +' ' Text +'egg' Name.Namespace +'(' Punctuation +'length' Name +',' Punctuation +' ' Text +'start' Name +'=' Operator +'-' Operator +'1000' Literal.Number +',' Punctuation +' ' Text +'end' Name +'=' Operator +'-' Operator +'1000' Literal.Number +')' Punctuation +' ' Text +'{' Punctuation +'\n' Text + +' ' Text +'rotate_extrude' Name.Builtin +'(' Punctuation +')' Punctuation +'\n' Text + +' ' Text +'translate' Name.Builtin +'(' Punctuation +'[' Punctuation +'0' Literal.Number +',' Punctuation +' ' Text +'-' Operator +'length' Name +'/' Operator +'2' Literal.Number +',' Punctuation +' ' Text +'0' Literal.Number +']' Punctuation +')' Punctuation +'\n' Text + +' ' Text +'rotate' Name.Builtin +'(' Punctuation +'[' Punctuation +'0' Literal.Number +',' Punctuation +' ' Text +'0' Literal.Number +',' Punctuation +' ' Text +'90' Literal.Number +']' Punctuation +')' Punctuation +'\n' Text + +' ' Text +'polygon' Name.Builtin +'(' Punctuation +'egg_half_poly' Name +'(' Punctuation +'\n' Text + +' ' Text +'length' Name +',' Punctuation +'\n' Text + +' ' Text +'start' Name +' ' Text +'=' Operator +'=' Operator +' ' Text +'-' Operator +'1000' Literal.Number +' ' Text +'?' Operator +' ' Text +'0' Literal.Number +' ' Text +':' Punctuation +' ' Text +'start' Name +',' Punctuation +'\n' Text + +' ' Text +'end' Name +' ' Text +'=' Operator +'=' Operator +' ' Text +'-' Operator +'1000' Literal.Number +' ' Text +'?' Operator +' ' Text +'length' Name +' ' Text +':' Punctuation +' ' Text +'end' Name +'\n' Text + +' ' Text +')' Punctuation +')' Punctuation +';' Punctuation +'\n' Text + +'}' Punctuation +'\n' Text + +'\n' Text + +'function' Keyword.Declaration +' ' Text +'egg_half_poly' Name.Function +'(' Punctuation +'length' Name +',' Punctuation +' ' Text +'start' Name +',' Punctuation +' ' Text +'end' Name +')' Punctuation +' ' Text +'=' Operator +'\n' Text + +' ' Text +'concat' Name.Builtin +'(' Punctuation +'\n' Text + +' ' Text +'[' Punctuation +'[' Punctuation +'start' Name +',' Punctuation +' ' Text +'0' Literal.Number +']' Punctuation +']' Punctuation +',' Punctuation +'\n' Text + +' ' Text +'[' Punctuation +'for' Keyword +' ' Text +'(' Punctuation +'x' Name +'=' Operator +'[' Punctuation +'start' Name +' ' Text +':' Punctuation +' ' Text +'(' Punctuation +'release' Name +' ' Text +'?' Operator +' ' Text +'0.5' Literal.Number +' ' Text +':' Punctuation +' ' Text +'1' Literal.Number +')' Punctuation +' ' Text +':' Punctuation +' ' Text +'end' Name +']' Punctuation +')' Punctuation +' ' Text +'[' Punctuation +'x' Name +',' Punctuation +' ' Text +'egg_eq' Name +'(' Punctuation +'x' Name +' ' Text +'-' Operator +' ' Text +'length' Name +'/' Operator +'2' Literal.Number +',' Punctuation +' ' Text +'length' Name +')' Punctuation +']' Punctuation +']' Punctuation +',' Punctuation +'\n' Text + +' ' Text +'[' Punctuation +'[' Punctuation +'end' Name +',' Punctuation +' ' Text +'0' Literal.Number +']' Punctuation +']' Punctuation +'\n' Text + +' ' Text +')' Punctuation +';' Punctuation +'\n' Text + +'\n' Text + +'function' Keyword.Declaration +' ' Text +'egg_eq' Name.Function +'(' Punctuation +'x' Name +',' Punctuation +' ' Text +'length' Name +')' Punctuation +' ' Text +'=' Operator +'\n' Text + +' ' Text +'length' Name +' ' Text +'/' Operator +' ' Text +'1.25' Literal.Number +' ' Text +'/' Operator +' ' Text +'2' Literal.Number +' ' Text +'*' Operator +' ' Text +'sqrt' Name.Builtin +'(' Punctuation +'\n' Text + +' ' Text +'(' Punctuation +'length' Name +' ' Text +'*' Operator +' ' Text +'length' Name +' ' Text +'-' Operator +' ' Text +'4' Literal.Number +' ' Text +'*' Operator +' ' Text +'x' Name +' ' Text +'*' Operator +' ' Text +'x' Name +')' Punctuation +' ' Text +'/' Operator +'\n' Text + +' ' Text +'(' Punctuation +'length' Name +' ' Text +'*' Operator +' ' Text +'length' Name +' ' Text +'+' Operator +' ' Text +'8' Literal.Number +' ' Text +'*' Operator +' ' Text +'egg_w' Name +' ' Text +'*' Operator +' ' Text +'x' Name +' ' Text +'+' Operator +' ' Text +'4' Literal.Number +' ' Text +'*' Operator +' ' Text +'egg_w' Name +' ' Text +'*' Operator +' ' Text +'egg_w' Name +')' Punctuation +'\n' Text + +' ' Text +')' Punctuation +';' Punctuation +'\n' Text From 61075c0497010a39cd7aca312c5adb4873f6f322 Mon Sep 17 00:00:00 2001 From: Nikolay Antipov Date: Tue, 6 Jun 2023 08:20:32 +0200 Subject: [PATCH 2/5] Format and doc adjustments (#886) Co-authored-by: Jean Abou-Samra --- pygments/lexers/openscad.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/pygments/lexers/openscad.py b/pygments/lexers/openscad.py index e1a508d47b..90ee8dee6c 100644 --- a/pygments/lexers/openscad.py +++ b/pygments/lexers/openscad.py @@ -15,6 +15,10 @@ class OpenScadLexer(RegexLexer): + """For openSCAD code. + + .. versionadded:: 2.16.0 + """ name = "OpenSCAD" aliases = ["openscad"] filenames = ["*.scad"] @@ -23,8 +27,8 @@ class OpenScadLexer(RegexLexer): "root": [ (r"[^\S\n]+", Text), (r"\n", Text), - (r"//(\n|[\w\W]*?[^\\]\n)", Comment.Singleline), - (r"/(\\\n)?[*][\w\W]*?[*](\\\n)?/", Comment.Mutliline), + (r"//(.|\n)*?(? Date: Tue, 13 Jun 2023 07:32:12 +0000 Subject: [PATCH 3/5] Documentation; imports optimization; regex adjustments (#886) --- pygments/lexers/_mapping.py | 2 +- pygments/lexers/openscad.py | 71 ++++-- tests/snippets/openscad/test_basic.txt | 312 +++++++++++++++---------- 3 files changed, 246 insertions(+), 139 deletions(-) diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 76d278382f..4703705ec4 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -353,7 +353,7 @@ 'OocLexer': ('pygments.lexers.ooc', 'Ooc', ('ooc',), ('*.ooc',), ('text/x-ooc',)), 'OpaLexer': ('pygments.lexers.ml', 'Opa', ('opa',), ('*.opa',), ('text/x-opa',)), 'OpenEdgeLexer': ('pygments.lexers.business', 'OpenEdge ABL', ('openedge', 'abl', 'progress'), ('*.p', '*.cls'), ('text/x-openedge', 'application/x-openedge')), - 'OpenScadLexer': ('pygments.lexers.openscad', 'OpenSCAD', ('openscad',), ('*.scad',), ()), + 'OpenScadLexer': ('pygments.lexers.openscad', 'OpenSCAD', ('openscad',), ('*.scad',), ('application/x-openscad',)), 'OutputLexer': ('pygments.lexers.special', 'Text output', ('output',), (), ()), 'PacmanConfLexer': ('pygments.lexers.configs', 'PacmanConf', ('pacmanconf',), ('pacman.conf',), ()), 'PanLexer': ('pygments.lexers.dsls', 'Pan', ('pan',), ('*.pan',), ()), diff --git a/pygments/lexers/openscad.py b/pygments/lexers/openscad.py index 90ee8dee6c..c58b7a8934 100644 --- a/pygments/lexers/openscad.py +++ b/pygments/lexers/openscad.py @@ -8,8 +8,8 @@ :license: BSD, see LICENSE for details. """ -from pygments.lexer import RegexLexer, bygroups -from pygments.token import Text, Comment, Punctuation, Operator, Keyword, Name, Literal +from pygments.lexer import RegexLexer, bygroups, words, include +from pygments.token import Text, Comment, Punctuation, Operator, Keyword, Name, Number, Whitespace, Literal, String __all__ = ['OpenScadLexer'] @@ -20,19 +20,20 @@ class OpenScadLexer(RegexLexer): .. versionadded:: 2.16.0 """ name = "OpenSCAD" + url = "https://openscad.org/" aliases = ["openscad"] filenames = ["*.scad"] + mimetypes = ["application/x-openscad"] tokens = { "root": [ - (r"[^\S\n]+", Text), - (r"\n", Text), - (r"//(.|\n)*?(?=|>|&&|\|\|", Operator), (r"\$(f[asn]|t|vp[rtd]|children)", Operator), (r"(undef|PI)\b", Keyword.Constant), ( @@ -44,22 +45,54 @@ class OpenScadLexer(RegexLexer): bygroups(Keyword.Namespace, Whitespace, Name.Namespace)), (r"(function)(\s*)([^\s\(]+)", bygroups(Keyword.Declaration, Whitespace, Name.Function)), - (r"\b(true|false)\b", Literal), - (r"\b(function|module|include|use|for|intersection_for|if|else|return)\b", - Keyword), - ( - r"\b(circle|square|polygon|text|sphere|cube|cylinder|polyhedron|translate|rotate|scale|resize|mirror|multmatrix|color|offset|hull|minkowski|union|difference|intersection|abs|sign|sin|cos|tan|acos|asin|atan|atan2|floor|round|ceil|ln|log|pow|sqrt|exp|rands|min|max|concat|lookup|str|chr|search|version|version_num|norm|cross|parent_module|echo|import|import_dxf|dxf_linear_extrude|linear_extrude|rotate_extrude|surface|projection|render|dxf_cross|dxf_dim|let|assign|len)\b", - Name.Builtin, + (words(("true", "false"), prefix=r"\b", suffix=r"\b"), Literal), + (words(( + "function", "module", "include", "use", "for", + "intersection_for", "if", "else", "return" + ), prefix=r"\b", suffix=r"\b"), Keyword + ), + (words(( + "circle", "square", "polygon", "text", "sphere", "cube", + "cylinder", "polyhedron", "translate", "rotate", "scale", + "resize", "mirror", "multmatrix", "color", "offset", "hull", + "minkowski", "union", "difference", "intersection", "abs", + "sign", "sin", "cos", "tan", "acos", "asin", "atan", "atan2", + "floor", "round", "ceil", "ln", "log", "pow", "sqrt", "exp", + "rands", "min", "max", "concat", "lookup", "str", "chr", + "search", "version", "version_num", "norm", "cross", + "parent_module", "echo", "import", "import_dxf", + "dxf_linear_extrude", "linear_extrude", "rotate_extrude", + "surface", "projection", "render", "dxf_cross", + "dxf_dim", "let", "assign", "len" + ), prefix=r"\b", suffix=r"\b"), + Name.Builtin ), - (r"\bchildren\b", Name.Builtin.Pseudo), - (r""(\\\\|\\"|[^"])*"", String.Double), - (r"-?\d+(\.\d+)?(e[+-]?\d+)?", Literal.Number), - (r"[a-zA-Z_]\w*", Name), + (words(("children"), prefix=r"\b", suffix=r"\b"), Name.Builtin.Pseudo), + (r'""".*?"""', String.Double), + (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double), + (r"-?\d+(\.\d+)?(e[+-]?\d+)?", Number), + (r"\w+", Name), ], "includes": [ ( - r"(<)([^>]*)(>)", + r"(<)([^>]*)(>)", bygroups(Punctuation, Comment.PreprocFile, Punctuation), ), ], + 'comment': [ + (r':param: [a-zA-Z_]\w*|:returns?:|(FIXME|MARK|TODO):', + Comment.Special) + ], + 'comment-single': [ + (r'\n', Text, '#pop'), + include('comment'), + (r'[^\n]', Comment.Single) + ], + 'comment-multi': [ + include('comment'), + (r'[^*/]', Comment.Multiline), + (r'/\*', Comment.Multiline, '#push'), + (r'\*/', Comment.Multiline, '#pop'), + (r'[*/]', Comment.Multiline) + ], } diff --git a/tests/snippets/openscad/test_basic.txt b/tests/snippets/openscad/test_basic.txt index 9a6c37ac71..d51484823c 100644 --- a/tests/snippets/openscad/test_basic.txt +++ b/tests/snippets/openscad/test_basic.txt @@ -1,5 +1,7 @@ ---input--- +include + /* Multiline comment */ @@ -7,6 +9,7 @@ egg(1500); // Single line comment +// TODO: add a line module egg(length, start=-1000, end=-1000) { rotate_extrude() @@ -34,337 +37,408 @@ function egg_eq(x, length) = ---tokens--- -'/*\n Multiline comment\n*/' Comment.Mutliline -'\n' Text - -'\n' Text +'include' Keyword.Namespace +' ' Text +'<' Punctuation +'threads.scad' Comment.PreprocFile +'>' Punctuation +'\n' Text.Whitespace + +'\n' Text.Whitespace + +'/*' Comment.Multiline +'\n' Comment.Multiline + +' ' Comment.Multiline +' ' Comment.Multiline +' ' Comment.Multiline +'M' Comment.Multiline +'u' Comment.Multiline +'l' Comment.Multiline +'t' Comment.Multiline +'i' Comment.Multiline +'l' Comment.Multiline +'i' Comment.Multiline +'n' Comment.Multiline +'e' Comment.Multiline +' ' Comment.Multiline +'c' Comment.Multiline +'o' Comment.Multiline +'m' Comment.Multiline +'m' Comment.Multiline +'e' Comment.Multiline +'n' Comment.Multiline +'t' Comment.Multiline +'\n' Comment.Multiline + +'*/' Comment.Multiline +'\n' Text.Whitespace + +'\n' Text.Whitespace 'egg' Name '(' Punctuation '1500' Literal.Number ')' Punctuation ';' Punctuation +'\n' Text.Whitespace + +'\n' Text.Whitespace + +'//' Comment.Single +' ' Comment.Single +'S' Comment.Single +'i' Comment.Single +'n' Comment.Single +'g' Comment.Single +'l' Comment.Single +'e' Comment.Single +' ' Comment.Single +'l' Comment.Single +'i' Comment.Single +'n' Comment.Single +'e' Comment.Single +' ' Comment.Single +'c' Comment.Single +'o' Comment.Single +'m' Comment.Single +'m' Comment.Single +'e' Comment.Single +'n' Comment.Single +'t' Comment.Single '\n' Text +'//' Comment.Single +' ' Comment.Single +'TODO:' Comment.Special +' ' Comment.Single +'a' Comment.Single +'d' Comment.Single +'d' Comment.Single +' ' Comment.Single +'a' Comment.Single +' ' Comment.Single +'l' Comment.Single +'i' Comment.Single +'n' Comment.Single +'e' Comment.Single '\n' Text -'// Single line comment\n' Comment.Singleline - -'\n' Text +'\n' Text.Whitespace 'module' Keyword.Namespace -' ' Text +' ' Text.Whitespace 'egg' Name.Namespace '(' Punctuation 'length' Name ',' Punctuation -' ' Text +' ' Text.Whitespace 'start' Name '=' Operator '-' Operator '1000' Literal.Number ',' Punctuation -' ' Text +' ' Text.Whitespace 'end' Name '=' Operator '-' Operator '1000' Literal.Number ')' Punctuation -' ' Text +' ' Text.Whitespace '{' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'rotate_extrude' Name.Builtin '(' Punctuation ')' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'translate' Name.Builtin '(' Punctuation '[' Punctuation '0' Literal.Number ',' Punctuation -' ' Text +' ' Text.Whitespace '-' Operator 'length' Name '/' Operator '2' Literal.Number ',' Punctuation -' ' Text +' ' Text.Whitespace '0' Literal.Number ']' Punctuation ')' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'rotate' Name.Builtin '(' Punctuation '[' Punctuation '0' Literal.Number ',' Punctuation -' ' Text +' ' Text.Whitespace '0' Literal.Number ',' Punctuation -' ' Text +' ' Text.Whitespace '90' Literal.Number ']' Punctuation ')' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'polygon' Name.Builtin '(' Punctuation 'egg_half_poly' Name '(' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'length' Name ',' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'start' Name -' ' Text +' ' Text.Whitespace '=' Operator '=' Operator -' ' Text +' ' Text.Whitespace '-' Operator '1000' Literal.Number -' ' Text +' ' Text.Whitespace '?' Operator -' ' Text +' ' Text.Whitespace '0' Literal.Number -' ' Text +' ' Text.Whitespace ':' Punctuation -' ' Text +' ' Text.Whitespace 'start' Name ',' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'end' Name -' ' Text +' ' Text.Whitespace '=' Operator '=' Operator -' ' Text +' ' Text.Whitespace '-' Operator '1000' Literal.Number -' ' Text +' ' Text.Whitespace '?' Operator -' ' Text +' ' Text.Whitespace 'length' Name -' ' Text +' ' Text.Whitespace ':' Punctuation -' ' Text +' ' Text.Whitespace 'end' Name -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace ')' Punctuation ')' Punctuation ';' Punctuation -'\n' Text +'\n' Text.Whitespace '}' Punctuation -'\n' Text +'\n' Text.Whitespace -'\n' Text +'\n' Text.Whitespace 'function' Keyword.Declaration -' ' Text +' ' Text.Whitespace 'egg_half_poly' Name.Function '(' Punctuation 'length' Name ',' Punctuation -' ' Text +' ' Text.Whitespace 'start' Name ',' Punctuation -' ' Text +' ' Text.Whitespace 'end' Name ')' Punctuation -' ' Text +' ' Text.Whitespace '=' Operator -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'concat' Name.Builtin '(' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace '[' Punctuation '[' Punctuation 'start' Name ',' Punctuation -' ' Text +' ' Text.Whitespace '0' Literal.Number ']' Punctuation ']' Punctuation ',' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace '[' Punctuation 'for' Keyword -' ' Text +' ' Text.Whitespace '(' Punctuation 'x' Name '=' Operator '[' Punctuation 'start' Name -' ' Text +' ' Text.Whitespace ':' Punctuation -' ' Text +' ' Text.Whitespace '(' Punctuation 'release' Name -' ' Text +' ' Text.Whitespace '?' Operator -' ' Text +' ' Text.Whitespace '0.5' Literal.Number -' ' Text +' ' Text.Whitespace ':' Punctuation -' ' Text +' ' Text.Whitespace '1' Literal.Number ')' Punctuation -' ' Text +' ' Text.Whitespace ':' Punctuation -' ' Text +' ' Text.Whitespace 'end' Name ']' Punctuation ')' Punctuation -' ' Text +' ' Text.Whitespace '[' Punctuation 'x' Name ',' Punctuation -' ' Text +' ' Text.Whitespace 'egg_eq' Name '(' Punctuation 'x' Name -' ' Text +' ' Text.Whitespace '-' Operator -' ' Text +' ' Text.Whitespace 'length' Name '/' Operator '2' Literal.Number ',' Punctuation -' ' Text +' ' Text.Whitespace 'length' Name ')' Punctuation ']' Punctuation ']' Punctuation ',' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace '[' Punctuation '[' Punctuation 'end' Name ',' Punctuation -' ' Text +' ' Text.Whitespace '0' Literal.Number ']' Punctuation ']' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace ')' Punctuation ';' Punctuation -'\n' Text +'\n' Text.Whitespace -'\n' Text +'\n' Text.Whitespace 'function' Keyword.Declaration -' ' Text +' ' Text.Whitespace 'egg_eq' Name.Function '(' Punctuation 'x' Name ',' Punctuation -' ' Text +' ' Text.Whitespace 'length' Name ')' Punctuation -' ' Text +' ' Text.Whitespace '=' Operator -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace 'length' Name -' ' Text +' ' Text.Whitespace '/' Operator -' ' Text +' ' Text.Whitespace '1.25' Literal.Number -' ' Text +' ' Text.Whitespace '/' Operator -' ' Text +' ' Text.Whitespace '2' Literal.Number -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'sqrt' Name.Builtin '(' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace '(' Punctuation 'length' Name -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'length' Name -' ' Text +' ' Text.Whitespace '-' Operator -' ' Text +' ' Text.Whitespace '4' Literal.Number -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'x' Name -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'x' Name ')' Punctuation -' ' Text +' ' Text.Whitespace '/' Operator -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace '(' Punctuation 'length' Name -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'length' Name -' ' Text +' ' Text.Whitespace '+' Operator -' ' Text +' ' Text.Whitespace '8' Literal.Number -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'egg_w' Name -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'x' Name -' ' Text +' ' Text.Whitespace '+' Operator -' ' Text +' ' Text.Whitespace '4' Literal.Number -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'egg_w' Name -' ' Text +' ' Text.Whitespace '*' Operator -' ' Text +' ' Text.Whitespace 'egg_w' Name ')' Punctuation -'\n' Text +'\n' Text.Whitespace -' ' Text +' ' Text.Whitespace ')' Punctuation ';' Punctuation -'\n' Text +'\n' Text.Whitespace From 93bd3765a4b6ffaca92c806087d415e107e34577 Mon Sep 17 00:00:00 2001 From: Nikolay Antipov Date: Wed, 14 Jun 2023 15:27:18 +0200 Subject: [PATCH 4/5] Adjust regex for comments; handle the children builtin (#886) Co-authored-by: Jean Abou-Samra --- pygments/lexers/openscad.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygments/lexers/openscad.py b/pygments/lexers/openscad.py index c58b7a8934..ee5b4716a4 100644 --- a/pygments/lexers/openscad.py +++ b/pygments/lexers/openscad.py @@ -67,7 +67,7 @@ class OpenScadLexer(RegexLexer): ), prefix=r"\b", suffix=r"\b"), Name.Builtin ), - (words(("children"), prefix=r"\b", suffix=r"\b"), Name.Builtin.Pseudo), + (r"\bchildren\b", Name.Builtin.Pseudo), (r'""".*?"""', String.Double), (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double), (r"-?\d+(\.\d+)?(e[+-]?\d+)?", Number), @@ -86,11 +86,11 @@ class OpenScadLexer(RegexLexer): 'comment-single': [ (r'\n', Text, '#pop'), include('comment'), - (r'[^\n]', Comment.Single) + (r'[^\n]+', Comment.Single) ], 'comment-multi': [ include('comment'), - (r'[^*/]', Comment.Multiline), + (r'[^*/]+', Comment.Multiline), (r'/\*', Comment.Multiline, '#push'), (r'\*/', Comment.Multiline, '#pop'), (r'[*/]', Comment.Multiline) From d2c84bfe8e53904fcc3e83c70444954b4e6f145b Mon Sep 17 00:00:00 2001 From: Nikolay Antipov Date: Wed, 14 Jun 2023 13:31:04 +0000 Subject: [PATCH 5/5] Single whitespace expression (#886) --- pygments/lexers/openscad.py | 1 - tests/snippets/openscad/test_basic.txt | 59 ++------------------------ 2 files changed, 3 insertions(+), 57 deletions(-) diff --git a/pygments/lexers/openscad.py b/pygments/lexers/openscad.py index ee5b4716a4..3d16977fb0 100644 --- a/pygments/lexers/openscad.py +++ b/pygments/lexers/openscad.py @@ -28,7 +28,6 @@ class OpenScadLexer(RegexLexer): tokens = { "root": [ (r"[^\S\n]+", Whitespace), - (r"\n", Whitespace), (r'//', Comment.Single, 'comment-single'), (r'/\*', Comment.Multiline, 'comment-multi'), (r"[{}\[\]\(\),;:]", Punctuation), diff --git a/tests/snippets/openscad/test_basic.txt b/tests/snippets/openscad/test_basic.txt index d51484823c..c1d2f3def3 100644 --- a/tests/snippets/openscad/test_basic.txt +++ b/tests/snippets/openscad/test_basic.txt @@ -47,29 +47,7 @@ function egg_eq(x, length) = '\n' Text.Whitespace '/*' Comment.Multiline -'\n' Comment.Multiline - -' ' Comment.Multiline -' ' Comment.Multiline -' ' Comment.Multiline -'M' Comment.Multiline -'u' Comment.Multiline -'l' Comment.Multiline -'t' Comment.Multiline -'i' Comment.Multiline -'l' Comment.Multiline -'i' Comment.Multiline -'n' Comment.Multiline -'e' Comment.Multiline -' ' Comment.Multiline -'c' Comment.Multiline -'o' Comment.Multiline -'m' Comment.Multiline -'m' Comment.Multiline -'e' Comment.Multiline -'n' Comment.Multiline -'t' Comment.Multiline -'\n' Comment.Multiline +'\n Multiline comment\n' Comment.Multiline '*/' Comment.Multiline '\n' Text.Whitespace @@ -86,42 +64,11 @@ function egg_eq(x, length) = '\n' Text.Whitespace '//' Comment.Single -' ' Comment.Single -'S' Comment.Single -'i' Comment.Single -'n' Comment.Single -'g' Comment.Single -'l' Comment.Single -'e' Comment.Single -' ' Comment.Single -'l' Comment.Single -'i' Comment.Single -'n' Comment.Single -'e' Comment.Single -' ' Comment.Single -'c' Comment.Single -'o' Comment.Single -'m' Comment.Single -'m' Comment.Single -'e' Comment.Single -'n' Comment.Single -'t' Comment.Single +' Single line comment' Comment.Single '\n' Text '//' Comment.Single -' ' Comment.Single -'TODO:' Comment.Special -' ' Comment.Single -'a' Comment.Single -'d' Comment.Single -'d' Comment.Single -' ' Comment.Single -'a' Comment.Single -' ' Comment.Single -'l' Comment.Single -'i' Comment.Single -'n' Comment.Single -'e' Comment.Single +' TODO: add a line' Comment.Single '\n' Text '\n' Text.Whitespace