Swift Parser Combinator Framework
帕西
Swift Parser Combinator Framework
除了简单的组合器之外,Parsey 还支持源位置/范围跟踪、
回溯预防和自定义错误消息。
特征
-
组合器接口
|
、、、、 组合子运算符~~
~~>
<~~
^^
-
词法分析原语:
Lexer.whitespace
, , ...Lexer.signedInteger
-
类似正则表达式的组合器:
- 的后缀。
.+
.many()
- 例:
let arrayLiteral = "[" ~~> expression.+ <~~ "]"
- 例:
- 的后缀。
.*
.manyOrNone()
- 例:
let classDef = (attribute | method).*
- 例:
- 的后缀。
.?
.optional()
- 例:
let declaration = "let" ~~> id ~~ (":" ~~> type).? ~~ ("=" ~~> expression)
- 例:
- 的后缀。
+
.manyConcatenated()
- 例:
let skippedSpaces = (Lexer.space | Lexer.tab)+
- 例:
- 的中缀。
+
.concatenatingResult(with:)
- 例:
let type = Lexer.upperLetter + Lexer.letter*
- 例:
Lexer.regex(_:)
用于直接应用正则表达式。- 例:
let id = Lexer.regex("[a-zA-Z][a-zA-Z0-9]*")
- 例:
- 的后缀。
-
防止回溯
.!
后缀运算符或.nonbacktracking()
-
错误消息的分析器标记
<!--
运算符或.tagged(_:)
-
包含源位置的丰富错误消息
- 例如:
Parse failure at 2:4 ---- (+ %% 1 -20) 2 3) ^~~~~~~~~~~~~~ Expecting an expression, but found "%"
-
Source range tracking
^^^
operator or.mapParse(_:)
- For example, S-expression gets parsed to
the following range-tracked AST:\n(+ \n\n(+ +1 -20) 2 3)
Expr:(2:1..<4:16):[ ID:(2:2..<2:3):+, Expr:(4:1..<4:11):[ ID:(4:2..<4:3):+, Int:(4:4..<4:6):1, Int:(4:7..<4:10):-20], Int:(4:12..<4:13):2, Int:(4:14..<4:15):3]
Requirements
-
Swift 3
-
Any operating system
Package
To use it in your Swift project, add the following dependency to your
Swift package description file.
⚙ Examples
0️⃣ An LLVM Compiler Frontend written in Swift using Parsey
1️⃣ Parse Left-associative Infix Expressions with Operator Precedence
2️⃣ Parse S-Expressions
3️⃣ Parse S-Expressions with Source Range Tracking
Dependency
License
MIT License