Skip to content

Commit

Permalink
Update JSON parsing code to support Read inputs
Browse files Browse the repository at this point in the history
Changelog: changed
  • Loading branch information
yorickpeterse committed Nov 3, 2024
1 parent c4cf183 commit 36f9af1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
60 changes: 35 additions & 25 deletions src/idoc/ir.inko
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@ import std.clone (Clone)
import std.cmp (Compare, Ordering)
import std.fs.file (ReadOnlyFile)
import std.fs.path (Path)
import std.io (Read)
import std.json (Error, ErrorKind, PullParser)
import std.range (InclusiveRange)
import std.sync (Channel)

let META = '$meta.json'

fn read_file(path: Path) -> Result[ByteArray, Error] {
let bytes = ByteArray.new

try ReadOnlyFile.new(path).then(fn (f) { f.read_all(bytes) }).map_error(
fn (e) { Error(kind: ErrorKind.Read(e), offset: 0) },
)

Result.Ok(bytes)
}

# Parses a stream of bytes into an `InclusiveRange`.
fn parse_range(
parser: mut PullParser,
fn parse_range[T: mut + Read](
parser: mut PullParser[T],
range: mut InclusiveRange,
) -> Result[Nil, Error] {
try parser.object(fn (o) {
Expand Down Expand Up @@ -70,8 +61,8 @@ class Location {
Location(InclusiveRange.new(1, 1), InclusiveRange.new(1, 1))
}

fn static parse(
parser: mut PullParser,
fn static parse[T: mut + Read](
parser: mut PullParser[T],
location: mut Location,
) -> Result[Nil, Error] {
try parser.object(fn (o) {
Expand Down Expand Up @@ -164,8 +155,11 @@ class Module {
traits: [],
)

let bytes = try read_file(path)
let parser = PullParser.new(bytes)
let file = try ReadOnlyFile.new(path).map_error(fn (e) {
Error(kind: ErrorKind.Read(e), offset: 0)
})

let parser = PullParser.new(file)

try parser.object(fn (o) {
o.string('name', fn (v) { mod.name = v })
Expand Down Expand Up @@ -225,8 +219,10 @@ class Metadata {

fn static parse(directory: ref Path) -> Result[Metadata, Error] {
let path = directory.join(META)
let bytes = try read_file(path)
let parser = PullParser.new(bytes)
let file = try ReadOnlyFile.new(path).map_error(fn (e) {
Error(kind: ErrorKind.Read(e), offset: 0)
})
let parser = PullParser.new(file)
let meta = Metadata(readme: Markdown.new)

try parser.object(fn (o) {
Expand Down Expand Up @@ -256,7 +252,9 @@ class Constant {
# The documentation of the symbol.
let @documentation: Markdown

fn static parse(parser: mut PullParser) -> Result[Constant, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Constant, Error] {
let val = Constant(
name: '',
location: Location.default,
Expand Down Expand Up @@ -332,7 +330,9 @@ class Field {
# The documentation of the symbol.
let @documentation: Markdown

fn static parse(parser: mut PullParser) -> Result[Field, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Field, Error] {
let val = Field(
name: '',
location: Location.default,
Expand Down Expand Up @@ -381,7 +381,9 @@ class Constructor {
# The documentation of the symbol.
let @documentation: Markdown

fn static parse(parser: mut PullParser) -> Result[Constructor, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Constructor, Error] {
let val = Constructor(
name: '',
location: Location.default,
Expand Down Expand Up @@ -436,7 +438,9 @@ class Method {
# The documentation of the symbol.
let @documentation: Markdown

fn static parse(parser: mut PullParser) -> Result[Method, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Method, Error] {
let val = Method(
name: '',
file: ''.to_path,
Expand Down Expand Up @@ -578,7 +582,9 @@ class Implementation {
# A boolean indicating if the implemented trait is public or not.
let @public: Bool

fn static parse(parser: mut PullParser) -> Result[Implementation, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Implementation, Error] {
let imp = Implementation(module: '', name: '', type: '', public: false)

try parser.object(fn (o) {
Expand Down Expand Up @@ -642,7 +648,9 @@ class Class {
# The traits implemented by this class.
let @implemented_traits: Array[Implementation]

fn static parse(parser: mut PullParser) -> Result[Class, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Class, Error] {
let val = Class(
name: '',
location: Location.default,
Expand Down Expand Up @@ -762,7 +770,9 @@ class Trait {
# The implementations of this trait.
let @implementations: Array[Implementation]

fn static parse(parser: mut PullParser) -> Result[Trait, Error] {
fn static parse[T: mut + Read](
parser: mut PullParser[T],
) -> Result[Trait, Error] {
let val = Trait(
name: '',
location: Location.default,
Expand Down
5 changes: 4 additions & 1 deletion src/idoc/markdown.inko
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import builder.html
import idoc.html (adjust_headings)
import markdown
import std.clone (Clone)
import std.io (Read)
import std.json
import wobsite.markdown (SyntaxHighlight)

Expand All @@ -13,7 +14,9 @@ class Markdown {
Markdown(markdown.Document.new)
}

fn static parse(parser: mut json.PullParser) -> Result[Markdown, json.Error] {
fn static parse[T: mut + Read](
parser: mut json.PullParser[T],
) -> Result[Markdown, json.Error] {
let start = try parser.start_of_next_value

parser.string.then(fn (md) {
Expand Down
23 changes: 14 additions & 9 deletions test/idoc/test_ir.inko
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import idoc.ir
import idoc.markdown (Markdown)
import markdown
import std.cmp (Ordering)
import std.io (Buffer)
import std.json (PullParser)
import std.range (InclusiveRange)
import std.sync (Channel)
Expand All @@ -11,6 +12,10 @@ fn two_paragraphs -> Markdown {
Markdown(markdown.Document.parse('first\n\nsecond').get)
}

fn pull(input: String) -> PullParser[Buffer[String]] {
PullParser.new(Buffer.new(input))
}

fn empty_class(kind: ir.ClassKind) -> ir.Class {
ir.Class(
name: 'A',
Expand All @@ -29,7 +34,7 @@ fn empty_class(kind: ir.ClassKind) -> ir.Class {

fn pub tests(t: mut Tests) {
t.test('parse_range', fn (t) {
let parser = PullParser.new('{"start":2,"end":3}')
let parser = pull('{"start":2,"end":3}')
let range = InclusiveRange.new(2, 3)
let res = ir.parse_range(parser, range)

Expand All @@ -46,7 +51,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Location.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'{ "lines": { "start": 1, "end": 2 }, "columns": { "start": 3, "end": 4 } }',
)

Expand Down Expand Up @@ -233,7 +238,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Constant.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"name": "A",
Expand Down Expand Up @@ -359,7 +364,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Field.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"name": "foo",
Expand Down Expand Up @@ -406,7 +411,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Constructor.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"name": "A",
Expand Down Expand Up @@ -450,7 +455,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Method.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"name": "foo",
Expand Down Expand Up @@ -636,7 +641,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Implementation.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"module": "std.foo",
Expand Down Expand Up @@ -683,7 +688,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Class.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"name": "A",
Expand Down Expand Up @@ -949,7 +954,7 @@ fn pub tests(t: mut Tests) {
})

t.ok('Trait.parse', fn (t) {
let parser = PullParser.new(
let parser = pull(
'
{
"name": "A",
Expand Down
9 changes: 7 additions & 2 deletions test/idoc/test_markdown.inko
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import idoc.markdown (Markdown)
import markdown
import std.io (Buffer)
import std.json (Json, PullParser)
import std.test (Tests)

fn pull(input: String) -> PullParser[Buffer[String]] {
PullParser.new(Buffer.new(input))
}

fn parse(string: String) -> Markdown {
Markdown.parse(PullParser.new(Json.String(string).to_string)).get
Markdown.parse(pull(Json.String(string).to_string)).get
}

fn pub tests(t: mut Tests) {
t.ok('Markdown.parse', fn (t) {
let parser = PullParser.new('"hello **world**"')
let parser = pull('"hello **world**"')
let md = try Markdown.parse(parser)

t.equal(
Expand Down

0 comments on commit 36f9af1

Please sign in to comment.