Modify moonscript lexer to get correct syntax highlighting

of interpolated strings
This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 20:18:05 -07:00
parent dbc71b4c0b
commit d22283905a
8 changed files with 226 additions and 5 deletions

View File

@ -1,3 +1,5 @@
$render{"lexer-test"}
$render{"templates/wares"}
<h2>Posts</h2>

10
lexer-test.html Normal file
View File

@ -0,0 +1,10 @@
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Thing</span>
<span class="nv">name:</span> <span class="s2">&quot;</span><span class="s">unknown&quot;</span>
<span class="k">class</span> <span class="nc">Person</span> <span class="k">extends</span> <span class="nc">Thing</span>
<span class="nv">say_name:</span> <span class="nf">=&gt;</span> <span class="nb">print</span> <span class="s2">&quot;</span><span class="s">Hel#lo, I am </span><span class="si">#{</span><span class="vc">@name</span><span class="si">}</span><span class="s">!&quot;</span>
<span class="k">with</span> <span class="nc">Person</span><span class="o">!</span>
<span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">MoonScript&quot;</span>
<span class="o">\</span><span class="n">say_name</span><span class="o">!</span>
</pre></div>

35
moonscript.css Normal file
View File

@ -0,0 +1,35 @@
.nb {
color: #F69385; }
/* strings */
.s, .s1, .s2, .se {
color: #F1BF8E; }
/* proper names, self */
.nc, .vc, .bp {
color: #99CBCA; }
/* true, false, nil */
.kc {
color: #B3EFE5; }
/* function lit, braces, parens */
.nf, .kt {
color: #B0D89C; }
/* operators */
.o, .si {
color: #F277A1; }
.nv {
color: #F277A1; }
/* keywords */
.k, .kd {
color: #BB84B4; }
.c1, .c2 {
color: #929292; }
.m, .mi, .mf, .mh {
color: #9D8FF2; }

89
moonscript.py Normal file
View File

@ -0,0 +1,89 @@
from pygments.lexer import default, combined, include, words
from pygments.lexers.scripting import LuaLexer
from pygments.token import *
def all_lua_builtins():
from pygments.lexers._lua_builtins import MODULES
return [w for values in MODULES.values() for w in values]
class CustomLexer(LuaLexer):
"""
For MoonScript source code.
"""
name = 'MoonScript'
url = 'http://moonscript.org'
aliases = ['moonscript', 'moon']
filenames = ['*.moon']
mimetypes = ['text/x-moonscript', 'application/x-moonscript']
version_added = '1.5'
tokens = {
'root': [
(r'#!(.*?)$', Comment.Preproc),
default('base')
],
'base': [
('--.*$', Comment.Single),
(r'(?i)(\d*\.\d+|\d+\.\d*)(e[+-]?\d+)?', Number.Float),
(r'(?i)\d+e[+-]?\d+', Number.Float),
(r'(?i)0x[0-9a-f]*', Number.Hex),
(r'\d+', Number.Integer),
(r'\n', Whitespace),
(r'[^\S\n]+', Text),
(r'(?s)\[(=*)\[.*?\]\1\]', String),
(r'(->|=>)', Name.Function),
(r':[a-zA-Z_]\w*', Name.Variable),
(r'(==|!=|~=|<=|>=|\.\.\.|\.\.|[=+\-*/%^<>#!.\\:])', Operator),
(r'[;,]', Punctuation),
(r'[\[\]{}()]', Keyword.Type),
(r'[a-zA-Z_]\w*:', Name.Variable),
(words((
'class', 'extends', 'if', 'then', 'super', 'do', 'with',
'import', 'export', 'while', 'elseif', 'return', 'for', 'in',
'from', 'when', 'using', 'else', 'and', 'or', 'not', 'switch',
'break'), suffix=r'\b'),
Keyword),
(r'(true|false|nil)\b', Keyword.Constant),
(r'(and|or|not)\b', Operator.Word),
(r'(self)\b', Name.Builtin.Pseudo),
(r'@@?([a-zA-Z_]\w*)?', Name.Variable.Class),
(r'[A-Z]\w*', Name.Class), # proper name
(words(all_lua_builtins(), suffix=r"\b"), Name.Builtin),
(r'[A-Za-z_]\w*', Name),
("'", String.Single, combined('stringescape', 'sqs')),
('"', String.Double, combined('stringescape', 'dqs')),
],
'stringescape': [
(r'''\\([abfnrtv\\"']|\d{1,3})''', String.Escape)
],
'strings': [
(r'[^#\\\'"]+', String),
# note that all coffee script strings are multi-line.
# hashmarks, quotes and backslashes must be parsed one at a time
],
'interpoling_string': [
(r'\}', String.Interpol, "#pop"),
include('base')
],
'dqs': [
(r'"', String, '#pop'),
(r'\\.|\'', String), # double-quoted string don't need ' escapes
(r'#\{', String.Interpol, "interpoling_string"),
(r'#', String),
include('strings')
],
'sqs': [
(r"'", String, '#pop'),
(r'#|\\.|"', String), # single quoted strings don't need " escapses
include('strings')
]
}
def get_tokens_unprocessed(self, text):
# set . as Operator instead of Punctuation
for index, token, value in LuaLexer.get_tokens_unprocessed(self, text):
if token == Punctuation and value == ".":
token = Operator
yield index, token, value

73
pygments.css Normal file
View File

@ -0,0 +1,73 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.hll { background-color: #ffffcc }
.c { color: #3D7B7B; font-style: italic } /* Comment */
.err { border: 1px solid #FF0000 } /* Error */
.k { color: #008000; font-weight: bold } /* Keyword */
.o { color: #666666 } /* Operator */
.ch { color: #3D7B7B; font-style: italic } /* Comment.Hashbang */
.cm { color: #3D7B7B; font-style: italic } /* Comment.Multiline */
.cp { color: #9C6500 } /* Comment.Preproc */
.cpf { color: #3D7B7B; font-style: italic } /* Comment.PreprocFile */
.c1 { color: #3D7B7B; font-style: italic } /* Comment.Single */
.cs { color: #3D7B7B; font-style: italic } /* Comment.Special */
.gd { color: #A00000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #E40000 } /* Generic.Error */
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
.gi { color: #008400 } /* Generic.Inserted */
.go { color: #717171 } /* Generic.Output */
.gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.gt { color: #0044DD } /* Generic.Traceback */
.kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.kp { color: #008000 } /* Keyword.Pseudo */
.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.kt { color: #B00040 } /* Keyword.Type */
.m { color: #666666 } /* Literal.Number */
.s { color: #BA2121 } /* Literal.String */
.na { color: #687822 } /* Name.Attribute */
.nb { color: #008000 } /* Name.Builtin */
.nc { color: #0000FF; font-weight: bold } /* Name.Class */
.no { color: #880000 } /* Name.Constant */
.nd { color: #AA22FF } /* Name.Decorator */
.ni { color: #717171; font-weight: bold } /* Name.Entity */
.ne { color: #CB3F38; font-weight: bold } /* Name.Exception */
.nf { color: #0000FF } /* Name.Function */
.nl { color: #767600 } /* Name.Label */
.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.nt { color: #008000; font-weight: bold } /* Name.Tag */
.nv { color: #19177C } /* Name.Variable */
.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mb { color: #666666 } /* Literal.Number.Bin */
.mf { color: #666666 } /* Literal.Number.Float */
.mh { color: #666666 } /* Literal.Number.Hex */
.mi { color: #666666 } /* Literal.Number.Integer */
.mo { color: #666666 } /* Literal.Number.Oct */
.sa { color: #BA2121 } /* Literal.String.Affix */
.sb { color: #BA2121 } /* Literal.String.Backtick */
.sc { color: #BA2121 } /* Literal.String.Char */
.dl { color: #BA2121 } /* Literal.String.Delimiter */
.sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.s2 { color: #BA2121 } /* Literal.String.Double */
.se { color: #AA5D1F; font-weight: bold } /* Literal.String.Escape */
.sh { color: #BA2121 } /* Literal.String.Heredoc */
.si { color: #A45A77; font-weight: bold } /* Literal.String.Interpol */
.sx { color: #008000 } /* Literal.String.Other */
.sr { color: #A45A77 } /* Literal.String.Regex */
.s1 { color: #BA2121 } /* Literal.String.Single */
.ss { color: #19177C } /* Literal.String.Symbol */
.bp { color: #008000 } /* Name.Builtin.Pseudo */
.fm { color: #0000FF } /* Name.Function.Magic */
.vc { color: #19177C } /* Name.Variable.Class */
.vg { color: #19177C } /* Name.Variable.Global */
.vi { color: #19177C } /* Name.Variable.Instance */
.vm { color: #19177C } /* Name.Variable.Magic */
.il { color: #666666 } /* Literal.Number.Integer.Long */

View File

@ -19,11 +19,11 @@ require(rend).cmd = "pandoc --mathjax >"
-- require(rend).cmd = "pandoc --mathjax -f json >"
-- Insert custom renderer in the first position so it will be preferred
table.insert Site.default_renderers, 1, rend
-- table.insert Site.default_renderers, 1, rend
-- Remove "pygments" plugin because it conflicts with pandoc syntax highlighting
Site.default_plugins = for v in *Site.default_plugins
if v\find "pygments" then continue else v
-- Site.default_plugins = for v in *Site.default_plugins
-- if v\find "pygments" then continue else v
-- from https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
get_files = (path, prefix=path) ->
@ -118,8 +118,8 @@ sitegen.create =>
} for path in *files
add "index.html", title: "Catalin Mititiuc"
add "blog.html", title: "Posts", target: "posts/index", template: "blog"
add_all files_from "docs"
-- add "blog.html", title: "Posts", target: "posts/index", template: "blog"
-- add_all files_from "docs"
-- replace post markdown yaml headers with moonscript headers
filter "docs", (body) =>
@ -129,3 +129,5 @@ sitegen.create =>
moonscript_header
copy "app.css"
-- copy "pygments.css"
copy "moonscript.css"

View File

@ -12,6 +12,7 @@
media="screen"
/>
<link rel="stylesheet" href="$root/app.css?$generate_date" />
<link rel="stylesheet" href="$root/moonscript.css?$generate_date" />
</head>
<body class="bg-white">
<header>

9
test.moon Normal file
View File

@ -0,0 +1,9 @@
class Thing
name: "unknown"
class Person extends Thing
say_name: => print "Hel#lo, I am #{@name}!"
with Person!
.name = "MoonScript"
\say_name!