Initial commit

This commit is contained in:
Dominic Grimm 2021-10-18 19:15:28 +02:00
commit f62793ff7b
6 changed files with 162 additions and 0 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

9
.gitignore vendored Normal file
View file

@ -0,0 +1,9 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf
# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2021 grimmigerFuchs <dominic.grimm@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

41
README.md Normal file
View file

@ -0,0 +1,41 @@
# sexpr
A small s-expression parser based on [fast-sexpr](https://www.npmjs.com/package/fast-sexpr).
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
sexpr:
github: grimmigerfuchs/sexpr
```
2. Run `shards install`
## Usage
```crystal
require "sexpr"
source = "
(test (arg1 arg2) something else)
(another object (with (nests (too))))
"
pp Sexpr.parse(source)
"
```
## Contributing
1. Fork it (<https://github.com/grimmigerfuchs/sexpr/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [grimmigerFuchs](https://github.com/grimmigerfuchs) - creator and maintainer

11
shard.yml Normal file
View file

@ -0,0 +1,11 @@
name: sexpr
description: |
A small s-expression parser based on fast-sexpr
version: 0.1.0
authors:
- grimmigerFuchs <dominic.grimm@gmail.com>
crystal: 1.2.0
license: MIT

71
src/sexpr.cr Normal file
View file

@ -0,0 +1,71 @@
# Based on fast-sexpr (https://www.npmjs.com/package/fast-sexpr)
module Sexpr
extend self
VERSION = {{ `shards version`.stringify.chomp }}
alias Node = String | Body
alias Body = Array(String) | Array(Node)
private class Wrapper
private property i = 0
private property source : String
private module Terms
STRING = /"/
SPACE = /\s/
LIST = /(#{SPACE})|[()]/
end
def initialize(@source : String)
end
private def get_char : Char?
if @i == @source.size
return nil
end
char = @source[@i]
@i += 1
char
end
private def read_value(is_terminator : Regex) : String
value = ""
while !is_terminator.matches?(strChar = get_char.to_s)
value += strChar
end
value
end
private def un_get_char
@i -= 1
end
def parse : Body
body = [] of Node
while char = get_char
if char == ')'
break
elsif char == '('
body << parse
elsif char == '"'
body << read_value(Terms::STRING)
elsif !Terms::SPACE.matches?(char.to_s)
un_get_char
body << read_value(Terms::LIST)
un_get_char
end
end
body
end
end
def parse(source : String) : Body
Wrapper.new(source).parse
end
end