├── .gitignore ├── gleam.toml ├── .github └── workflows │ └── test.yml ├── manifest.toml ├── README.md ├── CHANGELOG.md ├── test └── party_test.gleam ├── src └── party.gleam └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.beam 2 | *.ez 3 | build 4 | erl_crash.dump 5 | -------------------------------------------------------------------------------- /gleam.toml: -------------------------------------------------------------------------------- 1 | name = "party" 2 | version = "1.0.6" 3 | description = "A little parser combinator library in Gleam." 4 | 5 | licences = ["MPL-2.0"] 6 | repository = { type = "github", user = "RyanBrewer317", repo = "party" } 7 | gleam = ">= 0.32.0" 8 | # links = [{ title = "Website", href = "https://gleam.run" }] 9 | 10 | [dependencies] 11 | gleam_stdlib = "~> 0.36" # 0.36 <= version < 0.37 12 | 13 | [dev-dependencies] 14 | gleeunit = ">= 0.10.0" 15 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | pull_request: 9 | 10 | jobs: 11 | test: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: erlef/setup-beam@v1 15 | with: 16 | otp-version: "26.0.2" 17 | gleam-version: "1.0.0" 18 | rebar3-version: "3" 19 | # elixir-version: "1.15.4" 20 | - run: gleam format --check 21 | - run: gleam deps download 22 | - run: gleam test 23 | -------------------------------------------------------------------------------- /manifest.toml: -------------------------------------------------------------------------------- 1 | # This file was generated by Gleam 2 | # You typically do not need to edit this file 3 | 4 | packages = [ 5 | { name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" }, 6 | { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, 7 | ] 8 | 9 | [requirements] 10 | gleam_stdlib = { version = "~> 0.36" } 11 | gleeunit = { version = ">= 0.10.0" } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # party 2 | 3 | [![Package Version](https://img.shields.io/hexpm/v/party)](https://hex.pm/packages/party) 4 | [![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/party/) 5 | 6 | A little parser combinator library for Gleam! 7 | 8 | This is great for simple tasks. If you need something more heavyweight and capable, check out [Atto](https://hexdocs.pm/atto)! It's a great project, and it may be more complicated but it's got more features and better errors than Party, including some features that Party will intentionally not add for the foreseeable future. 9 | 10 | A little demo: 11 | ```gleam 12 | fn identstring() -> Parser(String, e) { 13 | use first <- do(lowercase_letter()) 14 | use rest <- do(many_concat(either(alphanum(), char("_")))) 15 | return(first <> rest) 16 | } 17 | ``` 18 | 19 | ## Installation 20 | 21 | If available on Hex this package can be added to your Gleam project: 22 | 23 | ```sh 24 | gleam add party 25 | ``` 26 | 27 | and its documentation can be found at . 28 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [1.0.6] - 2024-12-11 9 | 10 | ### Added 11 | 12 | - The `drop` combinator. Thank you [Drew Olson](https://github.com/drewolson)!! 13 | 14 | ## [1.0.5] - 2024-09-11 15 | 16 | ### Added 17 | 18 | - The `between` combinator, the `line` and `line_concat` combinators, and the `any_char` combinator. Thank you [Gonçalo Teixeira](https://github.com/tttardigrado)!! 19 | 20 | ## [1.0.4] - 2024-08-10 21 | 22 | ### Added 23 | 24 | - The `run` function, which `party` has been internally using to execute a parser in the most raw, detailed way. 25 | 26 | ## [1.0.3] - 2024-07-31 27 | 28 | ### Added 29 | 30 | - The `stateful_many` and `stateful_many1` combinators. 31 | 32 | ## [1.0.2] - 2024-04-05 33 | 34 | ### Added 35 | 36 | - 30ish tests. 37 | - More informative error messages for `not`. 38 | - The `until` combinator! 39 | 40 | ### Fixed 41 | 42 | - A bug in `try` that reported the wrong error position. 43 | 44 | ## [1.0.1] - 2024-03-28 45 | 46 | ### Fixed 47 | 48 | - Some `sep` and `sep1` bugs and poor performance. 49 | 50 | ### Changed 51 | 52 | - Relicensed to MPL-2.0 to be less viral. This is an increase in flexibility and in general projects that were depending on it under the GPLv3 license will not need to do anything to keep using it, as MPL-2.0 code can be used in GPLv3 codebases. Note, however, that I am not a lawyer and this should not be interpreted as legal advice. 53 | 54 | ## [1.0.0] - 2024-03-21 55 | 56 | ### Added 57 | 58 | - Parse errors return positions finally! 59 | - `digits` convenience function 60 | - `many_concat` convenience function 61 | - `many1_concat` convenience function 62 | - A little demo in the README 63 | 64 | ### Changed 65 | 66 | - Update dependencies for Gleam 1.0! 67 | - `alt` renamed to `either`. 68 | 69 | ### Fixed 70 | 71 | - Make pattern matching "exhaustive" with explicit panics for illegal inputs. This is required to make `party` compile with Gleam 1.0. -------------------------------------------------------------------------------- /test/party_test.gleam: -------------------------------------------------------------------------------- 1 | // This Source Code Form is subject to the terms of the Mozilla Public 2 | // License, v. 2.0. If a copy of the MPL was not distributed with this 3 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 | 5 | import gleam/int 6 | import gleam/list 7 | import gleam/string 8 | import gleeunit 9 | import gleeunit/should 10 | import party 11 | 12 | pub fn main() { 13 | gleeunit.main() 14 | } 15 | 16 | pub fn pos_test() { 17 | party.go( 18 | { 19 | use _ <- party.do(party.many(party.digit())) 20 | party.pos() 21 | }, 22 | "117e", 23 | ) 24 | |> should.equal(Ok(party.Position(1, 4))) 25 | } 26 | 27 | pub fn demorgan_test() { 28 | let characters = 29 | string.to_graphemes( 30 | "abcdefghijklmnopqrstuvwxyzABCDEGHIJKLMNOPQRSTUVWXYZ0123456789,.!@#$%^&*()\\|[]{};':\"<>/?`~", 31 | ) 32 | list.each(characters, fn(s) { 33 | party.go(party.seq(party.not(party.digit()), party.not(party.letter())), s) 34 | |> should.equal(party.go( 35 | party.not(party.either(party.digit(), party.letter())), 36 | s, 37 | )) 38 | }) 39 | } 40 | 41 | pub fn satisfy_test() { 42 | party.go(party.satisfy(fn(c) { c == "a" }), "a") 43 | |> should.equal(Ok("a")) 44 | party.go(party.satisfy(fn(c) { c == "b" }), "a") 45 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 46 | } 47 | 48 | pub fn any_char_test() { 49 | party.go(party.any_char(), "a") 50 | |> should.equal(Ok("a")) 51 | party.go(party.any_char(), "") 52 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "EOF"))) 53 | } 54 | 55 | pub fn between_test() { 56 | party.go( 57 | party.between(party.char("{"), party.char("a"), party.char("}")), 58 | "{a}", 59 | ) 60 | |> should.equal(Ok("a")) 61 | party.go( 62 | party.between(party.char("{"), party.char("a"), party.char("}")), 63 | "(a}", 64 | ) 65 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "("))) 66 | party.go( 67 | party.between(party.char("{"), party.char("a"), party.char("}")), 68 | "{b}", 69 | ) 70 | |> should.equal(Error(party.Unexpected(party.Position(1, 2), "b"))) 71 | party.go( 72 | party.between(party.char("{"), party.char("a"), party.char("}")), 73 | "{a)", 74 | ) 75 | |> should.equal(Error(party.Unexpected(party.Position(1, 3), ")"))) 76 | } 77 | 78 | pub fn line_test() { 79 | party.go(party.line(), "abcde fgh \n") 80 | |> should.equal(Ok(["a", "b", "c", "d", "e", " ", "f", "g", "h", " "])) 81 | party.go(party.line(), "abcde fgh ") 82 | |> should.equal(Error(party.Unexpected(party.Position(1, 11), "EOF"))) 83 | } 84 | 85 | pub fn line_concat_test() { 86 | party.go(party.line_concat(), "abcde fgh \n") 87 | |> should.equal(Ok("abcde fgh ")) 88 | party.go(party.line_concat(), "abcde fgh ") 89 | |> should.equal(Error(party.Unexpected(party.Position(1, 11), "EOF"))) 90 | } 91 | 92 | pub fn lowercase_letter_test() { 93 | party.go(party.lowercase_letter(), "a") 94 | |> should.equal(Ok("a")) 95 | party.go(party.lowercase_letter(), "A") 96 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "A"))) 97 | } 98 | 99 | pub fn uppercase_letter_test() { 100 | party.go(party.uppercase_letter(), "A") 101 | |> should.equal(Ok("A")) 102 | party.go(party.uppercase_letter(), "a") 103 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 104 | } 105 | 106 | pub fn letter_test() { 107 | party.go(party.letter(), "a") 108 | |> should.equal(Ok("a")) 109 | party.go(party.letter(), "A") 110 | |> should.equal(Ok("A")) 111 | party.go(party.letter(), "1") 112 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "1"))) 113 | } 114 | 115 | pub fn char_test() { 116 | let characters = 117 | string.to_graphemes( 118 | "abcdefghijklmnopqrstuvwxyzABCDEGHIJKLMNOPQRSTUVWXYZ0123456789,.!@#$%^&*()\\|[]{};':\"<>/?`~", 119 | ) 120 | list.each(characters, fn(s) { 121 | party.go(party.char(s), s) 122 | |> should.equal(Ok(s)) 123 | }) 124 | party.go(party.char("x"), "y") 125 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "y"))) 126 | } 127 | 128 | pub fn digit_test() { 129 | party.go(party.try(party.digit(), int.parse), "1") 130 | |> should.equal(Ok(1)) 131 | party.go(party.digit(), "2") 132 | |> should.equal(Ok("2")) 133 | party.go(party.digit(), "a") 134 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 135 | } 136 | 137 | pub fn digits_test() { 138 | party.go(party.digits(), "123") 139 | |> should.equal(Ok("123")) 140 | party.go(party.digits(), "a123") 141 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 142 | } 143 | 144 | pub fn either_test() { 145 | party.go(party.either(party.char("a"), party.char("b")), "a") 146 | |> should.equal(Ok("a")) 147 | party.go(party.either(party.char("a"), party.char("b")), "b") 148 | |> should.equal(Ok("b")) 149 | party.go(party.either(party.char("x"), party.char("y")), "z") 150 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "z"))) 151 | } 152 | 153 | pub fn choice_test() { 154 | party.go(party.choice([party.char("a"), party.char("b")]), "a") 155 | |> should.equal(Ok("a")) 156 | party.go(party.choice([party.char("a"), party.char("b")]), "b") 157 | |> should.equal(Ok("b")) 158 | party.go(party.choice([party.char("x"), party.char("y")]), "z") 159 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "z"))) 160 | } 161 | 162 | pub fn alphanum_test() { 163 | party.go(party.alphanum(), "a") 164 | |> should.equal(Ok("a")) 165 | party.go(party.alphanum(), "1") 166 | |> should.equal(Ok("1")) 167 | party.go(party.alphanum(), "#") 168 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "#"))) 169 | } 170 | 171 | pub fn whitespace_test() { 172 | party.go(party.whitespace(), " \t\n") 173 | |> should.equal(Ok(" \t\n")) 174 | party.go(party.whitespace(), "a") 175 | |> should.equal(Ok("")) 176 | } 177 | 178 | pub fn whitespace1_test() { 179 | party.go(party.whitespace1(), " \t\n") 180 | |> should.equal(Ok(" \t\n")) 181 | party.go(party.whitespace1(), "a") 182 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 183 | } 184 | 185 | pub fn many_test() { 186 | party.go(party.many(party.char("a")), "aaab") 187 | |> should.equal(Ok(["a", "a", "a"])) 188 | party.go(party.many(party.char("b")), "aaab") 189 | |> should.equal(Ok([])) 190 | } 191 | 192 | pub fn many_concat_test() { 193 | party.go(party.many_concat(party.char("a")), "aaab") 194 | |> should.equal(Ok("aaa")) 195 | party.go(party.many_concat(party.char("b")), "aaab") 196 | |> should.equal(Ok("")) 197 | } 198 | 199 | pub fn many1_test() { 200 | party.go(party.many1(party.char("a")), "aaab") 201 | |> should.equal(Ok(["a", "a", "a"])) 202 | party.go(party.many1(party.char("b")), "aaab") 203 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 204 | } 205 | 206 | pub fn many1_concat_test() { 207 | party.go(party.many1_concat(party.char("a")), "aaab") 208 | |> should.equal(Ok("aaa")) 209 | party.go(party.many1(party.char("b")), "aaab") 210 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 211 | } 212 | 213 | pub fn seq_test() { 214 | party.go(party.seq(party.char("a"), party.char("b")), "ab") 215 | |> should.equal(Ok("b")) 216 | party.go(party.seq(party.char("a"), party.char("b")), "aa") 217 | |> should.equal(Error(party.Unexpected(party.Position(1, 2), "a"))) 218 | party.go(party.seq(party.char("a"), party.char("b")), "b") 219 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "b"))) 220 | } 221 | 222 | pub fn sep_test() { 223 | party.go(party.sep(party.char("a"), by: party.char(",")), "a,a") 224 | |> should.equal(Ok(["a", "a"])) 225 | party.go(party.sep(party.char("a"), by: party.char(",")), "aa") 226 | |> should.equal(Ok(["a"])) 227 | party.go(party.sep(party.char("a"), by: party.char(",")), "") 228 | |> should.equal(Ok([])) 229 | } 230 | 231 | pub fn sep1_test() { 232 | party.go(party.sep1(party.char("a"), by: party.char(",")), "a,a") 233 | |> should.equal(Ok(["a", "a"])) 234 | party.go(party.sep1(party.char("a"), by: party.char(",")), "aa") 235 | |> should.equal(Ok(["a"])) 236 | party.go(party.sep1(party.char("a"), by: party.char(",")), "") 237 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "EOF"))) 238 | } 239 | 240 | pub fn map_test() { 241 | party.go(party.map(party.char("a"), fn(c) { Ok(c) }), "a") 242 | |> should.equal(Ok(Ok("a"))) 243 | party.go(party.map(party.char("a"), fn(c) { Ok(c) }), "b") 244 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "b"))) 245 | } 246 | 247 | pub fn try_test() { 248 | party.go(party.try(party.digit(), int.parse), "1") 249 | |> should.equal(Ok(1)) 250 | party.go(party.try(party.digit(), int.parse), "a") 251 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 252 | party.go(party.try(party.digit(), fn(_s) { Error("hi") }), "1") 253 | |> should.equal(Error(party.UserError(party.Position(1, 1), "hi"))) 254 | } 255 | 256 | pub fn error_map_test() { 257 | party.go(party.error_map(party.digit(), fn(_e) { "hi" }), "1") 258 | |> should.equal(Ok("1")) 259 | party.go(party.error_map(party.digit(), fn(_e) { "hi" }), "a") 260 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 261 | party.go( 262 | party.error_map(party.try(party.digit(), fn(_s) { Error("hi") }), fn(_e) { 263 | "hello" 264 | }), 265 | "1", 266 | ) 267 | |> should.equal(Error(party.UserError(party.Position(1, 1), "hello"))) 268 | } 269 | 270 | pub fn perhaps_test() { 271 | party.go(party.perhaps(party.digit()), "1") 272 | |> should.equal(Ok(Ok("1"))) 273 | party.go(party.perhaps(party.digit()), "a") 274 | |> should.equal(Ok(Error(Nil))) 275 | } 276 | 277 | pub fn all_test() { 278 | party.go(party.all([party.digit(), party.char("a"), party.alphanum()]), "1a2") 279 | |> should.equal(Ok("2")) 280 | party.go(party.all([party.digit(), party.char("a"), party.alphanum()]), "1x2") 281 | |> should.equal(Error(party.Unexpected(party.Position(1, 2), "x"))) 282 | } 283 | 284 | pub fn string_test() { 285 | party.go(party.string("hello"), "hello") 286 | |> should.equal(Ok("hello")) 287 | party.go(party.string("hello"), "world") 288 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "w"))) 289 | party.go(party.string("hello"), "hi") 290 | |> should.equal(Error(party.Unexpected(party.Position(1, 2), "i"))) 291 | } 292 | 293 | pub fn not_test() { 294 | party.go(party.not(party.digit()), "a") 295 | |> should.equal(Ok(Nil)) 296 | party.go(party.not(party.digit()), "1") 297 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "1"))) 298 | } 299 | 300 | pub fn end_test() { 301 | party.go(party.end(), "") 302 | |> should.equal(Ok(Nil)) 303 | party.go(party.end(), " ") 304 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), " "))) 305 | } 306 | 307 | pub fn lazy_test() { 308 | party.go(party.lazy(fn() { party.char("a") }), "a") 309 | |> should.equal(Ok("a")) 310 | } 311 | 312 | pub fn do_return_test() { 313 | { 314 | use a <- party.do(party.char("a")) 315 | party.return(a <> a) 316 | } 317 | |> party.go("a") 318 | |> should.equal(Ok("aa")) 319 | } 320 | 321 | pub fn do_drop_return_test() { 322 | { 323 | use a <- party.do(party.char("a")) 324 | use <- party.drop(party.char("*")) 325 | party.return(a <> a) 326 | } 327 | |> party.go("a*") 328 | |> should.equal(Ok("aa")) 329 | } 330 | 331 | pub fn fail_test() { 332 | party.go(party.fail(), "a") 333 | |> should.equal(Error(party.Unexpected(party.Position(1, 1), "a"))) 334 | } 335 | 336 | pub fn until_test() { 337 | party.go(party.until(do: party.char("a"), until: party.char("b")), "aab") 338 | |> should.equal(Ok(["a", "a"])) 339 | party.go(party.until(do: party.char("a"), until: party.char("b")), "aaa") 340 | |> should.equal(Error(party.Unexpected(party.Position(1, 4), "EOF"))) 341 | party.go(party.until(do: party.char("a"), until: party.char("b")), "ac") 342 | |> should.equal(Error(party.Unexpected(party.Position(1, 2), "c"))) 343 | } 344 | 345 | pub fn multiline_comment_test() { 346 | party.go( 347 | party.map( 348 | party.until(do: party.satisfy(fn(_) { True }), until: party.string("*/")), 349 | string.concat, 350 | ), 351 | "hello! * */ ", 352 | ) 353 | |> should.equal(Ok("hello! * ")) 354 | } 355 | 356 | pub fn stateful_many_test() { 357 | let stateful_char = fn(c) { 358 | party.char(c) 359 | |> party.map(fn(x) { fn(counter) { #(x, counter /. 2.0) } }) 360 | } 361 | party.go(party.stateful_many(100.0, stateful_char("a")), "aaab") 362 | |> should.equal(Ok(#(["a", "a", "a"], 12.5))) 363 | party.go(party.stateful_many(100.0, stateful_char("b")), "aaab") 364 | |> should.equal(Ok(#([], 100.0))) 365 | } 366 | -------------------------------------------------------------------------------- /src/party.gleam: -------------------------------------------------------------------------------- 1 | //// A simple parser combinator library. 2 | //// Party is stable, though breaking changes might come in a far-future 2.0 or 3.0 release. 3 | 4 | // This Source Code Form is subject to the terms of the Mozilla Public 5 | // License, v. 2.0. If a copy of the MPL was not distributed with this 6 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. 7 | 8 | import gleam/result 9 | import gleam/string 10 | 11 | /// The custom error type for the parser, 12 | /// which can itself be parameterized by a user-defined error type. 13 | /// The user-defined error type is useful for, for example, 14 | /// adding a `int.parse` call into your parser pipeline. 15 | /// See `try` for using this feature. 16 | pub type ParseError(e) { 17 | Unexpected(pos: Position, error: String) 18 | UserError(pos: Position, error: e) 19 | } 20 | 21 | /// The type for positions within a string. 22 | pub type Position { 23 | Position(row: Int, col: Int) 24 | } 25 | 26 | /// The parser type, parameterized by the type it parses and 27 | /// the user-defined error type it can return. 28 | pub opaque type Parser(a, e) { 29 | Parser( 30 | parse: fn(List(String), Position) -> 31 | Result(#(a, List(String), Position), ParseError(e)), 32 | ) 33 | } 34 | 35 | /// ADVANCED (exposes the internals of `party`) 36 | /// Apply a parser to a list of graphemes (holding on to extra result info that `party` typically threads for you). 37 | pub fn run( 38 | p: Parser(a, e), 39 | src: List(String), 40 | pos: Position, 41 | ) -> Result(#(a, List(String), Position), ParseError(e)) { 42 | case p { 43 | Parser(f) -> f(src, pos) 44 | } 45 | } 46 | 47 | /// Apply a parser to a string. 48 | pub fn go(p: Parser(a, e), src: String) -> Result(a, ParseError(e)) { 49 | case run(p, string.to_graphemes(src), Position(1, 1)) { 50 | Ok(#(x, _, _)) -> Ok(x) 51 | Error(e) -> Error(e) 52 | } 53 | } 54 | 55 | /// Get the current parser position. 56 | pub fn pos() -> Parser(Position, e) { 57 | Parser(fn(source, p) { Ok(#(p, source, p)) }) 58 | } 59 | 60 | /// Parse a character if it matches the predicate. 61 | pub fn satisfy(when pred: fn(String) -> Bool) -> Parser(String, e) { 62 | Parser(fn(source, pos) { 63 | let Position(row, col) = pos 64 | case source { 65 | [h, ..t] -> 66 | case pred(h) { 67 | True -> 68 | case h { 69 | "\n" -> Ok(#(h, t, Position(row + 1, 0))) 70 | _ -> Ok(#(h, t, Position(row, col + 1))) 71 | } 72 | False -> Error(Unexpected(pos, h)) 73 | } 74 | [] -> Error(Unexpected(pos, "EOF")) 75 | } 76 | }) 77 | } 78 | 79 | /// Parse a single character. 80 | pub fn any_char() -> Parser(String, e) { 81 | satisfy(fn(_) { True }) 82 | } 83 | 84 | /// Parse a lowercase letter. 85 | pub fn lowercase_letter() -> Parser(String, e) { 86 | satisfy(when: fn(c) { string.contains("abcdefghijklmnopqrstuvwxyz", c) }) 87 | } 88 | 89 | /// Parse an uppercase letter. 90 | pub fn uppercase_letter() -> Parser(String, e) { 91 | satisfy(when: fn(c) { string.contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", c) }) 92 | } 93 | 94 | /// Parse a lowercase or uppercase letter. 95 | pub fn letter() -> Parser(String, e) { 96 | either(lowercase_letter(), uppercase_letter()) 97 | } 98 | 99 | /// Parse a specific character. 100 | pub fn char(c) -> Parser(String, e) { 101 | satisfy(when: fn(c2) { c == c2 }) 102 | } 103 | 104 | /// Parse a digit. 105 | pub fn digit() -> Parser(String, e) { 106 | satisfy(fn(c) { string.contains("0123456789", c) }) 107 | } 108 | 109 | /// Parse a sequence of digits. 110 | pub fn digits() -> Parser(String, e) { 111 | many1_concat(digit()) 112 | } 113 | 114 | /// Parse the first parser, or the second if the first fails. 115 | pub fn either(p: Parser(a, e), q: Parser(a, e)) -> Parser(a, e) { 116 | Parser(fn(source, pos) { result.or(run(p, source, pos), run(q, source, pos)) }) 117 | } 118 | 119 | /// Parse `open`, followed by `p` and `close`. Returns the value returned by `p`. 120 | /// The values returned by `open` and `close` are discarded. 121 | pub fn between( 122 | open: Parser(_, e), 123 | p: Parser(a, e), 124 | close: Parser(_, e), 125 | ) -> Parser(a, e) { 126 | use _ <- do(open) 127 | use x <- do(p) 128 | use _ <- do(close) 129 | return(x) 130 | } 131 | 132 | /// Parse the rest of a line and return the array of parsed characters. 133 | /// The newline character at the end is discarded. 134 | pub fn line() -> Parser(List(String), e) { 135 | until(any_char(), char("\n")) 136 | } 137 | 138 | /// Parse the rest of a line and return the parsed characters as a String. 139 | /// The newline character at the end is discarded. 140 | pub fn line_concat() -> Parser(String, e) { 141 | line() 142 | |> map(string.concat) 143 | } 144 | 145 | /// Parse with the first parser in the list that doesn't fail. 146 | pub fn choice(ps: List(Parser(a, e))) -> Parser(a, e) { 147 | Parser(fn(source, pos) { 148 | case ps { 149 | [] -> panic as "choice doesn't accept an empty list of parsers" 150 | // TODO: should this be an Unexpected instead? 151 | [p] -> run(p, source, pos) 152 | [p, ..t] -> 153 | case run(p, source, pos) { 154 | Ok(#(x, r, pos2)) -> Ok(#(x, r, pos2)) 155 | Error(_) -> run(choice(t), source, pos) 156 | } 157 | } 158 | }) 159 | } 160 | 161 | /// Parse an alphanumeric character. 162 | pub fn alphanum() -> Parser(String, e) { 163 | either(digit(), letter()) 164 | } 165 | 166 | /// Parse zero or more whitespace characters. 167 | pub fn whitespace() -> Parser(String, e) { 168 | many_concat(choice([char(" "), char("\t"), char("\n")])) 169 | } 170 | 171 | /// Parse one or more whitespace characters. 172 | pub fn whitespace1() -> Parser(String, e) { 173 | many1_concat(choice([char(" "), char("\t"), char("\n")])) 174 | } 175 | 176 | /// Keep trying the parser until it fails, and return the array of parsed results. 177 | /// This cannot fail because it parses zero or more times! 178 | pub fn many(p: Parser(a, e)) -> Parser(List(a), e) { 179 | Parser(fn(source, pos) { 180 | case run(p, source, pos) { 181 | Error(_) -> Ok(#([], source, pos)) 182 | Ok(#(x, r, pos2)) -> 183 | result.map(run(many(p), r, pos2), fn(res) { 184 | #([x, ..res.0], res.1, res.2) 185 | }) 186 | } 187 | }) 188 | } 189 | 190 | /// Parse a certain string as many times as possible, returning everything that was parsed. 191 | /// This cannot fail because it parses zero or more times! 192 | pub fn many_concat(p: Parser(String, e)) -> Parser(String, e) { 193 | many(p) 194 | |> map(string.concat) 195 | } 196 | 197 | /// Keep trying the parser until it fails, and return the array of parsed results. 198 | /// This can fail, because it must parse successfully at least once! 199 | pub fn many1(p: Parser(a, e)) -> Parser(List(a), e) { 200 | Parser(fn(source, pos) { 201 | case run(p, source, pos) { 202 | Error(e) -> Error(e) 203 | Ok(#(x, r, pos2)) -> 204 | result.map(run(many(p), r, pos2), fn(res) { 205 | #([x, ..res.0], res.1, res.2) 206 | }) 207 | } 208 | }) 209 | } 210 | 211 | /// Parse a certain string as many times as possible, returning everything that was parsed. 212 | /// This can fail, because it must parse successfully at least once! 213 | pub fn many1_concat(p: Parser(String, e)) -> Parser(String, e) { 214 | many1(p) 215 | |> map(string.concat) 216 | } 217 | 218 | /// Do the first parser, ignore its result, then do the second parser. 219 | pub fn seq(p: Parser(a, e), q: Parser(b, e)) -> Parser(b, e) { 220 | use _ <- do(p) 221 | q 222 | } 223 | 224 | /// A version of `seq` for pleasant interplay with gleam's `use` syntax. 225 | /// example: 226 | /// ``` 227 | /// fn pair() -> Parser(#(String, String), e) { 228 | /// use a <- do(alphanum()) 229 | /// use <- drop(char(",")) 230 | /// use b <- do(alphanum()) 231 | /// return(#(a, b)) 232 | /// } 233 | /// ``` 234 | pub fn drop(p: Parser(a, e), f: fn() -> Parser(b, e)) -> Parser(b, e) { 235 | seq(p, lazy(f)) 236 | } 237 | 238 | /// Parse a sequence separated by the given separator parser. 239 | pub fn sep(parser: Parser(a, e), by s: Parser(b, e)) -> Parser(List(a), e) { 240 | use res <- do(perhaps(sep1(parser, by: s))) 241 | case res { 242 | Ok(sequence) -> return(sequence) 243 | Error(Nil) -> return([]) 244 | } 245 | } 246 | 247 | /// Parse a sequence separated by the given separator parser. 248 | /// This only succeeds if at least one element of the sequence was parsed. 249 | pub fn sep1(parser: Parser(a, e), by s: Parser(b, e)) -> Parser(List(a), e) { 250 | use first <- do(parser) 251 | use rest <- do(many(seq(s, parser))) 252 | return([first, ..rest]) 253 | } 254 | 255 | /// Do `p`, then apply `f` to the result if it succeeded. 256 | pub fn map(p: Parser(a, e), f: fn(a) -> b) -> Parser(b, e) { 257 | Parser(fn(source, pos) { 258 | case run(p, source, pos) { 259 | Ok(#(x, r, pos2)) -> Ok(#(f(x), r, pos2)) 260 | Error(e) -> Error(e) 261 | } 262 | }) 263 | } 264 | 265 | /// Do `p`, the apply `f` to the result if it succeeded. 266 | /// `f` itself can fail with the user-defined error type, 267 | /// and if it does the result is a `UserError` with the error. 268 | pub fn try(p: Parser(a, e), f: fn(a) -> Result(b, e)) -> Parser(b, e) { 269 | Parser(fn(source, pos) { 270 | case run(p, source, pos) { 271 | Ok(#(x, r, pos2)) -> 272 | case f(x) { 273 | Ok(a) -> Ok(#(a, r, pos2)) 274 | Error(e) -> Error(UserError(pos, e)) 275 | } 276 | Error(e) -> Error(e) 277 | } 278 | }) 279 | } 280 | 281 | /// Transform the user-defined error type 282 | /// with a user-provided conversion function. 283 | pub fn error_map(p: Parser(a, e), f: fn(e) -> f) -> Parser(a, f) { 284 | Parser(fn(source, pos) { 285 | case run(p, source, pos) { 286 | Ok(res) -> Ok(res) 287 | Error(e) -> 288 | case e { 289 | UserError(pos, e) -> Error(UserError(pos, f(e))) 290 | Unexpected(pos, s) -> Error(Unexpected(pos, s)) 291 | } 292 | } 293 | }) 294 | } 295 | 296 | /// Try running a parser, but still succeed (with `Error(Nil)`) if it failed. 297 | pub fn perhaps(p: Parser(a, e)) -> Parser(Result(a, Nil), e) { 298 | Parser(fn(source, pos) { 299 | case run(p, source, pos) { 300 | Ok(#(x, r, pos2)) -> Ok(#(Ok(x), r, pos2)) 301 | Error(_) -> Ok(#(Error(Nil), source, pos)) 302 | } 303 | }) 304 | } 305 | 306 | /// Do each parser in the list, returning the result of the last parser. 307 | pub fn all(ps: List(Parser(a, e))) -> Parser(a, e) { 308 | case ps { 309 | [p] -> p 310 | [h, ..t] -> { 311 | use _ <- do(h) 312 | all(t) 313 | } 314 | _ -> panic as "all(parsers) doesn't accept an empty list of parsers" 315 | } 316 | // TODO: should this be an Unexpected instead? 317 | } 318 | 319 | /// Parse an exact string of characters. 320 | pub fn string(s: String) -> Parser(String, e) { 321 | case string.pop_grapheme(s) { 322 | Ok(#(h, t)) -> { 323 | use c <- do(char(h)) 324 | use rest <- do(string(t)) 325 | return(c <> rest) 326 | } 327 | Error(_) -> return("") 328 | } 329 | } 330 | 331 | /// Negate a parser: if it succeeds, this fails, and vice versa. 332 | /// Example: `seq(string("if"), not(either(alphanum(), char("_"))))` 333 | pub fn not(p: Parser(a, e)) -> Parser(Nil, e) { 334 | Parser(fn(source, pos) { 335 | case run(p, source, pos) { 336 | Ok(_) -> 337 | case source { 338 | [h, ..] -> Error(Unexpected(pos, h)) 339 | _ -> Error(Unexpected(pos, "EOF")) 340 | } 341 | // todo: better error message here (add a label system) 342 | Error(_) -> Ok(#(Nil, source, pos)) 343 | } 344 | }) 345 | } 346 | 347 | /// Parses successfully only when at the end of the input string. 348 | pub fn end() -> Parser(Nil, e) { 349 | Parser(fn(source, pos) { 350 | case source { 351 | [] -> Ok(#(Nil, source, pos)) 352 | [h, ..] -> Error(Unexpected(pos, h)) 353 | } 354 | }) 355 | } 356 | 357 | /// Run a parser as normal, but the parser itself isn't evaluated until it is used. 358 | /// This is needed for recursive grammars, such as `E := n | E + E` where `n` is a number. 359 | /// Example: `lazy(digit)` instead of `digit()`. 360 | pub fn lazy(p: fn() -> Parser(a, e)) -> Parser(a, e) { 361 | Parser(fn(source, pos) { run(p(), source, pos) }) 362 | } 363 | 364 | /// A monadic bind for pleasant interplay with gleam's `use` syntax. 365 | /// example: 366 | /// ``` 367 | /// fn identifier() -> Parser(String, e) { 368 | /// use pos <- do(pos()) 369 | /// use first <- do(lowercase_letter()) 370 | /// use rest <- do(many(either(alphanum(), char("_")))) 371 | /// return(Ident(pos, first <> string.concat(rest))) 372 | /// } 373 | /// ``` 374 | pub fn do(p: Parser(a, e), f: fn(a) -> Parser(b, e)) -> Parser(b, e) { 375 | Parser(fn(source, pos) { 376 | case run(p, source, pos) { 377 | Ok(#(x, r, pos2)) -> run(f(x), r, pos2) 378 | Error(e) -> Error(e) 379 | } 380 | }) 381 | } 382 | 383 | /// A monadic return for pleasant interplay with gleam's `use` syntax. 384 | /// see `do` for more details and an example. 385 | /// This is redundant if the last `do` is a `map` instead. 386 | /// But I prefer using it, stylistically. 387 | pub fn return(x) { 388 | Parser(fn(source, pos) { Ok(#(x, source, pos)) }) 389 | } 390 | 391 | /// Immediately fail regardless of the next input. 392 | pub fn fail() -> Parser(a, e) { 393 | Parser(fn(source, pos) { 394 | case source { 395 | [] -> Error(Unexpected(pos, "EOF")) 396 | [h, ..] -> Error(Unexpected(pos, h)) 397 | } 398 | }) 399 | } 400 | 401 | /// Parse zero or more repetitions of a parser, collecting the results into a list. 402 | /// Stop when the terminator parser succeeds, even if the looping parser would also succeed. 403 | /// The terminator parser's results are consumed and discarded. 404 | /// The main motivator for `until` is multiline comments ending in `*/`, `-->`, `-}`, `*)`, etc. 405 | pub fn until( 406 | do p: Parser(a, e), 407 | until terminator: Parser(b, e), 408 | ) -> Parser(List(a), e) { 409 | either( 410 | { 411 | use _ <- do(terminator) 412 | return([]) 413 | }, 414 | { 415 | use first <- do(p) 416 | use rest <- do(until(p, terminator)) 417 | return([first, ..rest]) 418 | }, 419 | ) 420 | } 421 | 422 | /// A `many` parser that also gets to update some state with each success 423 | pub fn stateful_many( 424 | state: s, 425 | p: Parser(fn(s) -> #(a, s), e), 426 | ) -> Parser(#(List(a), s), e) { 427 | Parser(fn(source, pos) { 428 | case run(p, source, pos) { 429 | Error(_) -> Ok(#(#([], state), source, pos)) 430 | Ok(#(f, r, pos2)) -> { 431 | let #(x, s) = f(state) 432 | result.map(run(stateful_many(s, p), r, pos2), fn(res) { 433 | let #(#(rest, s2), r2, pos3) = res 434 | #(#([x, ..rest], s2), r2, pos3) 435 | }) 436 | } 437 | } 438 | }) 439 | } 440 | 441 | /// A `many1` parser that also gets to update some state with each success 442 | pub fn stateful_many1( 443 | state: s, 444 | p: Parser(fn(s) -> #(a, s), e), 445 | ) -> Parser(#(List(a), s), e) { 446 | Parser(fn(source, pos) { 447 | case run(p, source, pos) { 448 | Error(e) -> Error(e) 449 | Ok(#(f, r, pos2)) -> { 450 | let #(x, s) = f(state) 451 | result.map(run(stateful_many(s, p), r, pos2), fn(res) { 452 | let #(#(rest, s), r2, pos3) = res 453 | #(#([x, ..rest], s), r2, pos3) 454 | }) 455 | } 456 | } 457 | }) 458 | } 459 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. --------------------------------------------------------------------------------