珍珠 – 一个用 Gleam 编写的 Erlang 词法分析器和语法高亮器
Pearl – An Erlang lexer and syntax highlighter in Gleam

原始链接: https://github.com/GearsDatapacks/pearl

Pearl 是一个基于 Gleam 的 Erlang 代码词法分析器和语法高亮器。它建立在 glexer 和 just 之上,将 Erlang 源代码转换为用于解析或进一步处理的词法单元。 它提供了一个通用的 API,用于以 ANSI 颜色突出显示代码以在终端上显示,将其渲染为 HTML 以供 Web 浏览器使用,或生成“高亮词法单元”以进行自定义格式化。示例代码演示了使用所有三种方法词法分析、标记化和突出显示 Erlang 代码。 虽然 Pearl 旨在处理所有有效的 Erlang 程序,但其实现依赖于 Erlang 文档和现有的解析器,因为缺乏正式的语法规范。开发者欢迎错误报告和贡献,以提高其完整性和准确性——更多详细信息请访问 [https://hexdocs.pm/pearl](https://hexdocs.pm/pearl)。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Pearl – Gleam 编写的 Erlang 词法分析器和语法高亮器 (github.com/gearsdatapacks) 29 分,TheWiggles 1 天前 | 隐藏 | 过去 | 收藏 | 1 条评论 nifty_beaks 1 天前 [–] 我个人没有这方面的需求,但总是很高兴看到 Gleam 的文章出现。真是一种美妙的小语言。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

An Erlang lexer and syntax highlighter for Gleam!

Package Version Hex Docs

Pearl is a lexer and syntax highlighter for Erlang, written in Gleam. The lexer is based on glexer and just, allowing you to convert Erlang source code into tokens. There is also an API which allows you to highlight Erlang code using ansi colours, html or a custom format. Heavily inspired by contour.

import pearl

pub fn main() {
  let code = "
-module(hello).
-export([hello_world/0]).

hello_world() -> io:fwrite(\"Hello, world!\\n\").
"

  let lexer = pearl.new(code)
  // Lex syntax tokens for parsing or other uses
  let #(tokens, errors) = pearl.tokenise(lexer)
  let assert [] = errors
  parse_erlang(tokens)

  // Highlight with ansi codes to print in the terminal
  let highlighted = pearl.highlight_ansi(code)
  io.println(highlighted)

  // Render to html to show in the browser
  let html = pearl.highlight_html(code)
  io.println("<pre><code>" <> html <> "</code></pre>")

  // Convert to "highlighting tokens" to highlight in some other way
  let highlight_tokens = pearl.highlight_tokens(code)
  highlight_tokens_some_other_way(highlight_tokens)
}

Further documentation can be found at https://hexdocs.pm/pearl.

As far as I can tell, pearl can lex all valid Erlang programs. However, I couldn't find a technical specification for Erlang syntax so what I've implemented is based on the Erlang documentation and other Erlang parsers. If there is something missing, please open an issue and I'll implement it as soon as possible.

联系我们 contact @ memedata.com