{"id":21011575,"url":"https://github.com/zhuzilin/swiftpeg","last_synced_at":"2025-07-28T21:09:44.602Z","repository":{"id":106435208,"uuid":"329816180","full_name":"zhuzilin/SwiftPEG","owner":"zhuzilin","description":"A PEG parser generator written in swift 5.3.","archived":false,"fork":false,"pushed_at":"2021-01-19T05:22:30.000Z","size":13,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-11T02:53:48.867Z","etag":null,"topics":["parser","parser-generator","parsimonious","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhuzilin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-15T05:17:46.000Z","updated_at":"2025-04-30T11:14:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"c8a92080-8283-43df-a7c8-62ee83de4b4a","html_url":"https://github.com/zhuzilin/SwiftPEG","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/zhuzilin/SwiftPEG","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhuzilin%2FSwiftPEG","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhuzilin%2FSwiftPEG/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhuzilin%2FSwiftPEG/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhuzilin%2FSwiftPEG/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhuzilin","download_url":"https://codeload.github.com/zhuzilin/SwiftPEG/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhuzilin%2FSwiftPEG/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267585844,"owners_count":24111577,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["parser","parser-generator","parsimonious","swift"],"created_at":"2024-11-19T09:29:36.104Z","updated_at":"2025-07-28T21:09:44.580Z","avatar_url":"https://github.com/zhuzilin.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftPEG\n\nA PEG parser generator written in swift 5.3. The code structure and grammar are largely learnt from the excellent python package [parsimonious](https://github.com/erikrose/parsimonious). If you are doing some parsing using python, you should definitely check it out.\n\nThe nice part of this parser generator is that its PEG rule parser is also generated from a PEG syntax with a bootstrap manner, and the bootstrap hardcoding parser can also be generated from itself. The rule syntax is:\n\n```\n    rules = _ rule+\n    rule = label equals expression\n    equals = \"=\" _\n    literal = spaceless_literal _\n\n    spaceless_literal = ~\"\\\"[^\\\"\\\\\\\\]*(?:\\\\\\\\.[^\\\"\\\\\\\\]*)*\\\"\"is\n\n    expression = ored / sequence / term\n    or_term = \"/\" _ term\n    ored = term or_term+\n    sequence = term term+\n    not_term = \"!\" term _\n    lookahead_term = \"\u0026\" term _\n    term = not_term / lookahead_term / quantified / atom\n    quantified = atom quantifier\n    atom = reference / literal / regex / parenthesized\n    regex = \"~\" spaceless_literal ~\"[ilmsuxa]*\"i _\n    parenthesized = \"(\" _ expression \")\" _\n    quantifier = ~\"[*+?]\" _\n    reference = label !equals\n\n    label = ~\"[a-zA-Z_][a-zA-Z_0-9]*\" _\n\n    _ = meaninglessness*\n    meaninglessness = ~\"\\s+\" / comment\n    comment = ~\"#[^\\r\\n]*\"\n```\n\nNotice that the above syntax is the same as [parsimonious](https://github.com/erikrose/parsimonious).\n\nTo write a proper PEG syntax, please follow the [PEG syntax reference](https://www.gnu.org/software/guile/manual/html_node/PEG-Syntax-Reference.html).\n\n## Usage\n\nIn your `Package.swift`, add the following code to dependencies:\n\n```swift\n.package(name: \"SwiftPEG\", url: \"https://github.com/zhuzilin/SwiftPEG.git\", from: \"0.1.0\"),\n```\n\nAnd add `\"SwiftPEG\"` to target dependencies.\n\n## Example\n\nHere is an example of a simplified markdown parser.\n\n```swift\nlet markdownSyntax = #\"\"\"\n    raw_text = ~\"[^\\n]+\"\n    bold_text = (\"**\" raw_text \"**\") / (\"__\" raw_text \"__\")\n    text = (bold_text / raw_text)\n\n    h1 = \"# \" text\n    h2 = \"## \" text\n    h3 = \"### \" text\n    h4 = \"#### \" text\n    h5 = \"##### \" text\n    h6 = \"######\" text\n    header = (h6 / h5 / h4 / h3 / h2 / h1)\n\n    ordered_list = (~\"[0~9]+\\. \" text ~\"\\n\")+\n\n    unordered_list = (~\"[-*+] \" text ~\"\\n\")+\n\n    link = \"[\" raw_text \"]\" \"(\" raw_text \")\"\n\n    image = \"![\" raw_text \"]\" \"(\" raw_text \")\"\n\n    paragraph = (header / text)?\n    doc = (paragraph ~\"\\n\\n\")* paragraph\n\"\"\"#\n\n// Initialize the parser\nlet markdownParser: Grammar = Grammar(rules: markdownSyntax)\n// Get the AST root node from the parser with the name of the rule you defined in the syntax.\nlet ast: Node = grammar.parse(for: text, with: \"doc\")\n// Then do what ever you like with the AST\n...\n// Or your can use the simplified AST which only contains node with named rule\nlet simplifiedAst: SimplifiedNode = simplify(for: ast)\n...\n```\n\n## API\n\n### Grammar\n\n`Grammar` type has the following public interfaces:\n\n```swift\npublic class Grammar {\n    // Name dict of the parsing rules defined in the syntax\n    // It will be generated upon init.\n    // If it is empty it means there is some error in the syntax.\n    public var ruleDict: [String: Expression] = [:]\n    \n    public init(rules: String)\n    // Return nil if the parsing failed\n    public func parse(for text: String, with ruleName: String) -\u003e Node?\n}\n```\n\n### Node\n\n`Node` type has the following public interfaces:\n\n```swift\npublic struct Node: CustomStringConvertible, Equatable {\n    // The parser node used to parse this node\n    public let expr: Expression\n    public var name: String { expr.name }\n    // The children nodes\n    public var children: [Node] = []\n    // The matched text of this Node\n    public var text: String\n    // The matched range of this Node\n    public let start: String.Index\n    public let end: String.Index\n\n    public var description: String {\n        toString(withName: true)\n    }\n    public func toString(withName: Bool = false) -\u003e String\n\n  \tpublic static func ==(lhs: Node, rhs: Node) -\u003e Bool\n}\n```\n\n### SimplifiedNode\n\n`SimplifiedNode` type has the following interfaces:\n\n```swift\npublic struct SimplifiedNode: CustomStringConvertible {\n    public let name: String\n    // The children nodes\n    public var children: [SimplifiedNode] = []\n    // The matched text of this Node\n    public var text: String\n    // The matched range of this Node\n    public let start: String.Index\n    public let end: String.Index\n\n    public var description: String\n}\n```\n\n### Expression\n\nNormally you should not work with this type. If you have interest, please check `Expression.swift` for more information.\n\n## TODO\n\n- Support better error handling.\n- Optimize the performance with memoization.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhuzilin%2Fswiftpeg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhuzilin%2Fswiftpeg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhuzilin%2Fswiftpeg/lists"}