Skip to content

Commit

Permalink
[fix] currying
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Zen committed Nov 26, 2024
1 parent 82eb38d commit f92aa3a
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 69 deletions.
127 changes: 75 additions & 52 deletions src/new-parser.typ
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@
let (args, brace-level, processed-chars) = parse-argument-list(state.unfinished-param)
if brace-level == -1 { // parentheses are already closed on this line
state.state = "finished"
let curry = state.unfinished-param.slice(processed-chars).match(curry-matcher)
if curry != none {
state.curry = (name: curry.captures.first(), rest: state.unfinished-param.slice(processed-chars + curry.end))
}
// let curry = state.unfinished-param.slice(processed-chars).match(curry-matcher)
// if curry != none {
// state.curry = (name: curry.captures.first(), rest: state.unfinished-param.slice(processed-chars + curry.end))
// }
}
if args.len() > 0 and (state.unfinished-param.ends-with(",") or state.state == "finished") {
state.params.push((name: args.first(), desc-lines: state.unmatched-description))
Expand All @@ -229,23 +229,37 @@
return state
}

#{
let state = (
state: "running",
params: (),
unmatched-description: (),
unfinished-param: ""
// #{
// let state = (
// state: "running",
// params: (),
// unmatched-description: (),
// unfinished-param: ""
// )
// let lines = ("/// asd", "named: 3,", "/// -> int", "pos", ") = asd.with()", "").rev()
// // let lines = ("/// asd", "named: (", "fill: white, ", "cap: \"butt\")", ")").rev()
// while state.state == "running" and lines.len() > 0 {
// state = parameter-parser(state, lines.pop())
// }
// let kstate = state
// }
#let process-curry-info(info) = {
let pos = info.args
.filter(x => x.name.len() == 1)
.map(x => x.name.at(0))
let named = info.args
.filter(x => x.name.len() == 2)
.map(x => x.name).to-dict()

(
name: info.name,
pos: pos,
named: named
)
let lines = ("/// asd", "named: 3,", "/// -> int", "pos", ") = asd.with()", "").rev()
// let lines = ("/// asd", "named: (", "fill: white, ", "cap: \"butt\")", ")").rev()
while state.state == "running" and lines.len() > 0 {
state = parameter-parser(state, lines.pop())
}
let kstate = state
}



#let parse(src) = {
let lines = (src.split("\n") + ("",)).map(str.trim)

Expand Down Expand Up @@ -278,18 +292,19 @@
finished-definition = true
curry-info.args = param-parser.params
param-parser = param-parser-default
args = ()
} else {
args = param-parser.params
if "curry" in param-parser {
let curry = param-parser.curry
curry-info = (name: curry.name)
param-parser = param-parser-default
param-parser.state = "running"
param-parser = parameter-parser(param-parser, curry.rest)
if param-parser.state == "finished" {
finished-definition = true
param-parser = param-parser-default
}
// let curry = param-parser.curry
// curry-info = (name: curry.name)
// param-parser = param-parser-default
// param-parser.state = "running"
// param-parser = parameter-parser(param-parser, curry.rest)
// if param-parser.state == "finished" {
// finished-definition = true
// param-parser = param-parser-default
// }
} else {
finished-definition = true
param-parser = param-parser-default
Expand All @@ -305,7 +320,7 @@
if name != none {
definitions.push((name: name, description: desc-lines, args: args))
if curry-info != none {
definitions.at(-1).curry-info = curry-info
definitions.at(-1).parent = process-curry-info(curry-info)
curry-info = none
}
}
Expand All @@ -331,9 +346,17 @@
if match.captures.at(1) != "" { // it's a function
param-parser.state = "running"
param-parser = parameter-parser(param-parser, line.slice(match.end))
} else { // it's a variable
} else { // it's a variable or a function alias
args = none
finished-definition = true
let p = line.slice(match.end)

let curry = line.slice(match.end).match(curry-matcher)
if curry != none {
curry-info = (name: curry.captures.first())
param-parser = parameter-parser(param-parser, line.slice(match.end + curry.end))
// param-parser.curry = (name: curry.captures.first(), rest: state.unfinished-param.slice(processed-chars + curry.end))
}
}
}

Expand All @@ -357,30 +380,30 @@
variables: definitions.filter(x => "args" not in x),
)
}
#{
// #{


let src = ```
///Description
let func(
pos, // some comment
// let src = ```
// ///Description
// let func(
// pos, // some comment
named: 2 // another comment
)
```.text

assert.eq(
parse(src).functions,
(
(
name: "func",
description: "Description",
args: (
pos: (description: ""),
named: (description: "", default: "2"),
),
return-types: none
),
)
)
}
// named: 2 // another comment
// )
// ```.text
// assert.eq(
// parse(src).functions,
// (
// (
// name: "func",
// description: "Description",
// args: (
// pos: (description: ""),
// named: (description: "", default: "2"),
// ),
// return-types: none
// ),
// )
// )
// }
18 changes: 12 additions & 6 deletions src/parse-module.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let docs = function-docs.at(i)
if not "parent" in docs { continue }

let parent = docs.parent
let parent = docs.at("parent", default: none)
if parent == none { continue }

let parent-docs = function-docs.find(x => x.name == parent.name)
Expand Down Expand Up @@ -40,7 +40,12 @@
}


#let old-parse(content, label-prefix: "", require-all-parameters: false, enable-curried-functions: true) = {
#let old-parse(
content,
label-prefix: "",
require-all-parameters: false,
enable-curried-functions: true
) = {

let parse-info = (
label-prefix: label-prefix,
Expand All @@ -63,7 +68,8 @@
variable-docs.push(doc)
} else {
doc.parent = parent-info
doc.remove("type")
if "type" in doc { doc.remove("type") }
doc.args = (:)
function-docs.push(doc)
}
} else {
Expand Down Expand Up @@ -153,9 +159,9 @@
docs += new-parser.parse(content)
}
// TODO
// if enable-curried-functions {
// function-docs = resolve-parents(function-docs)
// }
if enable-curried-functions {
docs.functions = resolve-parents(docs.functions)
}


return docs
Expand Down
56 changes: 46 additions & 10 deletions tests/new-parser/currying.typ
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@

#import "/src/new-parser.typ": *
#import "/src/parse-module.typ": *

#let src = ```
/// Doc
#let aey(x) = text.with()
#let aey = text.with(1pt, weight: 200)
```.text
#assert.eq(
parse(src).functions,
parse-module(src).functions.at(0),
(
(
name: "aey",
description: "Doc",
args: (x: (description: ""),),
curry-info: (name: "text"),
return-types: none
name: "aey",
description: "Doc",
args: (:),
parent: (
name: "text",
pos: ("1pt",),
named: (weight: "200")
),
)
return-types: none
),
)
// Parent resolving
#let src = ```
/// -> any
let my-text(
/// The weight
/// -> int
weight: 400,
body
)

/// Doc
#let aey = my-text.with(weight: 200)
```.text
#assert.eq(
parse-module(src).functions.at(1),
(
name: "aey",
description: "Doc",
args: (
body: (description: ""),
weight: (description: "The weight", types: ("int",), default: "200")
),
parent: (
name: "my-text",
pos: (),
named: (weight: "200")
),
return-types: ("any",)
),
)
58 changes: 58 additions & 0 deletions tests/old-parser/currying.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#import "/src/parse-module.typ": *

#let parse-module = parse-module.with(old-syntax: true)
#let src = ```
/// Doc
#let aey = text.with(1pt, weight: 200)
```.text
#assert.eq(
parse-module(src).functions,
(
(
name: "aey",
description: " Doc\n\n",
args: (:),
parent: (
name: "text",
pos: ("1pt",),
named: (weight: "200")
),
),
)
)
// Parent resolving
#let src = ```
/// - weight (int): The weight
/// -> any
let my-text(
weight: 400,
body
)

/// Doc
#let aey = my-text.with(weight: 200)
```.text
#assert.eq(
parse-module(src).functions.at(1),
(
name: "aey",
description: " Doc\n\n",
parent: (
name: "my-text",
pos: (),
named: (weight: "200")
),
args: (
body: (:),
weight: (description: "The weight", types: ("int",), default: "200")
),
return-types: ("any",)
),
)
3 changes: 2 additions & 1 deletion tests/old-parser/test.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#set page(width: auto, height: auto, margin: 2pt)

#include "parse-argument-list.typ"
#include "parse-module.typ"
#include "parse-module.typ"
#include "currying.typ"

0 comments on commit f92aa3a

Please sign in to comment.