diff --git a/src/new-parser.typ b/src/new-parser.typ index f87e2ab..39fdc00 100644 --- a/src/new-parser.typ +++ b/src/new-parser.typ @@ -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)) @@ -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) @@ -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 @@ -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 } } @@ -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)) + } } } @@ -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 - ), - ) - ) -} \ No newline at end of file +// named: 2 // another comment +// ) +// ```.text + +// assert.eq( +// parse(src).functions, +// ( +// ( +// name: "func", +// description: "Description", +// args: ( +// pos: (description: ""), +// named: (description: "", default: "2"), +// ), +// return-types: none +// ), +// ) +// ) +// } \ No newline at end of file diff --git a/src/parse-module.typ b/src/parse-module.typ index 8ebc117..cedb395 100644 --- a/src/parse-module.typ +++ b/src/parse-module.typ @@ -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) @@ -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, @@ -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 { @@ -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 diff --git a/tests/new-parser/currying.typ b/tests/new-parser/currying.typ index b276d9a..9949aa2 100644 --- a/tests/new-parser/currying.typ +++ b/tests/new-parser/currying.typ @@ -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",) + ), ) diff --git a/tests/old-parser/currying.typ b/tests/old-parser/currying.typ new file mode 100644 index 0000000..7b47013 --- /dev/null +++ b/tests/old-parser/currying.typ @@ -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",) + ), +) + diff --git a/tests/old-parser/test.typ b/tests/old-parser/test.typ index fee58ff..3092693 100644 --- a/tests/old-parser/test.typ +++ b/tests/old-parser/test.typ @@ -1,4 +1,5 @@ #set page(width: auto, height: auto, margin: 2pt) #include "parse-argument-list.typ" -#include "parse-module.typ" \ No newline at end of file +#include "parse-module.typ" +#include "currying.typ" \ No newline at end of file