Skip to content

Commit

Permalink
feat(next): naive lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed Nov 20, 2024
1 parent 911b132 commit 46ca51f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion next/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_build
*.mo
*.mo
__pycache__
32 changes: 32 additions & 0 deletions next/_ext/lexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pygments.lexer import RegexLexer, words
import pygments.token as token
from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata

def setup(app: Sphinx) -> ExtensionMetadata:
app.add_lexer("moonbit", MoonBitLexer)
app.add_lexer("mbt", MoonBitLexer)
return {
"version": "0.1.0",
"parallel_read_safe": True,
"parallel_write_safe": True,
}

class MoonBitLexer(RegexLexer):
name = "MoonBit"

tokens = {
'root': [
(r"//.*$", token.Comment),
(words(('fn', 'if', 'else', 'while', 'for', 'loop', 'match', 'let', 'mut', 'trait', 'impl', 'with'), suffix="\s"), token.Keyword),
(words(('true', 'false'), suffix='\s'), token.Keyword),
(words(('Array', 'FixedArray', 'Int', 'Int64', 'UInt', 'UInt64', 'Option', 'Result', 'Byte', 'Bool'), suffix='\s'), token.Keyword),
(r"(=>)|(\|>)|(->)|[\(\)\{\}\[\]:,\.=+\-*/]", token.Punctuation),
(r"-?\d+(.\d+)?", token.Number),
(r"[a-zA-Z_][a-zA-Z0-9_]*", token.Name),
(r"\'.?\'", token.Literal),
(r"\"[^\"]*\"", token.String),
(r"#\|.*$", token.Text),
(r"[\s]", token.Whitespace),
]
}
6 changes: 5 additions & 1 deletion next/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ['myst_parser']
import sys
from pathlib import Path
sys.path.append(str(Path("_ext").resolve()))

extensions = ['myst_parser', 'lexer']

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', ".env", "README"]
Expand Down
2 changes: 1 addition & 1 deletion next/tutorial/tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This guide is intended for newcomers, and it's not meant to be a 5-minute quick tour. This article tries to be a succinct yet easy to understand guide
for those who haven't programmed in a way that MoonBit enables them to, that is, in a more modern, functional way.

See [the General Introduction](./README.md) if you want to straight delve into the language.
See [the General Introduction](../language/index.md) if you want to straight delve into the language.

## Installation

Expand Down

0 comments on commit 46ca51f

Please sign in to comment.