Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/astrada/ocaml-css-parser
A CSS parser written in OCaml
https://github.com/astrada/ocaml-css-parser
css ocaml parser
Last synced: 2 months ago
JSON representation
A CSS parser written in OCaml
- Host: GitHub
- URL: https://github.com/astrada/ocaml-css-parser
- Owner: astrada
- License: mit
- Created: 2018-12-01T17:55:02.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-09-01T18:43:59.000Z (over 3 years ago)
- Last Synced: 2023-03-12T16:15:25.885Z (almost 2 years ago)
- Topics: css, ocaml, parser
- Language: OCaml
- Size: 123 KB
- Stars: 32
- Watchers: 5
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ocaml-css-parser
================A CSS parser written in OCaml.
Parses a CSS string and produces an AST.
### Build
```bash
npm install -g esy
esy
# to build and run tests
esy dune runtest
```### Example
```ocaml
let css =
{|
{
color: red !important;
width: 100%;
}
|} in
let ast = Css.Parser.parse_stylesheet css in
(* ast is a value of type Css.Stylesheet.t defined in lib/types.mli *)
(* that looks like that:
([Rule.Style_rule
{Style_rule.prelude = ([], Location.none);
block =
([Declaration_list.Declaration
{Declaration.name = ("color", Location.none);
value = ([(Component_value.Ident "blue", Location.none)], Location.none);
important = (true, Location.none);
loc = Location.none;
};
{Declaration.name = ("width", Location.none);
value = ([(Component_value.Percentage "100", Location.none)], Location.none);
important = (false, Location.none);
loc = Location.none;
};
], Location.none);
loc = Location.none;
};
], Location.none)
*)
```### Remarks
Whitespaces and comments are discarded by the lexer, so they are not available
to the parser. An exception is made for significant whitespaces in rule
preludes, to disambiguate between selectors like `p :first-child` and
`p:first-child`. These whitespaces are replaced with `*` to keep CSS semantics
intact. So, e.g., `p :first-child` is parsed as `p *:first-child`, `p .class`
as `p *.class`, and `p #id` as `p *#id`.