Skip to content

Commit

Permalink
[fix] "//" in default arguments
Browse files Browse the repository at this point in the history
Fixes #40
Note that this still does not deal with `//` in content or raw elements
  • Loading branch information
Mc-Zen committed Jan 10, 2025
1 parent 0d05949 commit e752b87
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 55 deletions.
77 changes: 22 additions & 55 deletions src/new-parser.typ
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
return (arg.trim(),)
}
}

let skip-line = false

for c in text {
let ignore-char = false

if c == "\"" and previous-char != "\\" {
if literal-mode == none { literal-mode = "\"" }
else if literal-mode == "\"" { literal-mode = none }
}
if literal-mode == none {
} else if literal-mode == none {
if c == "(" { brace-level += 1 }
else if c == ")" { brace-level -= 1 }
else if c == "," and brace-level == 1 {
Expand All @@ -47,27 +48,29 @@
is-named = false
} else if c == ":" and brace-level == 1 {
is-named = true
} else if c == "/" and previous-char == "/" {
skip-line = true
arg = arg.slice(0, -1)
} else if c == "\n" {
skip-line = false
}
}
count-processed-chars += 1
if brace-level == 0 {
if arg.trim().len() > 0 {
if is-named {
let (name, value) = split-once(arg, ":").map(str.trim)
args.push((name, value))
args.push((name, value.replace("\n", "")))
} else {
arg = arg.trim()
arg = arg.trim().replace("\n", "")
args.push((arg,))
}
// arg = ""
}
break
}
if not ignore-char { arg += c }
if not (ignore-char or skip-line) { arg += c }
previous-char = c
}
// arg = arg.trim()
// if arg != "" { args.push(arg) }
return (
args: args,
brace-level: brace-level - 1,
Expand All @@ -91,6 +94,14 @@
parse-argument-list("a: 2, b: 3)"),
(args: (("a", "2"), ("b", "3")), brace-level: -1, processed-chars: 11)
)
#assert.eq(
parse-argument-list("a: 2 // 2\n)"),
(args: (("a", "2"),), brace-level: -1, processed-chars: 11)
)
#assert.eq(
parse-argument-list("a: 2, // 2\nb)"),
(args: (("a", "2"),("b",)), brace-level: -1, processed-chars: 13)
)


#let eval-doc-comment-test((line-number, line), label-prefix: "") = {
Expand Down Expand Up @@ -183,9 +194,6 @@
}


// #assert.eq(
// process-parameters(((arg: "myarg)", desc-lines: ()),)), ()
// )

#let process-definition(definition) = {
let (description, types) = parse-description-and-types(definition.description, label-prefix: "")
Expand All @@ -209,7 +217,7 @@
if line.starts-with("///") {
state.unmatched-description.push(line.slice(3))
} else {
state.unfinished-param += trim-trailing-comments(line)
state.unfinished-param += line + "\n"

let (args, brace-level, processed-chars) = parse-argument-list(state.unfinished-param)
if brace-level == -1 { // parentheses are already closed on this line
Expand All @@ -219,7 +227,7 @@
// 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") {
if args.len() > 0 and (state.unfinished-param.trim("\n").ends-with(",") or state.state == "finished") {
state.params.push((name: args.first(), desc-lines: state.unmatched-description))
state.unmatched-description = ()
state.params += args.slice(1).map(arg => (name: arg, desc-lines: ()))
Expand All @@ -229,21 +237,6 @@
return state
}

// #{
// 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)
Expand Down Expand Up @@ -380,30 +373,4 @@
variables: definitions.filter(x => "args" not in x),
)
}
// #{


// 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
// ),
// )
// )
// }
50 changes: 50 additions & 0 deletions tests/new-parser/issue_#40.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#import "/src/new-parser.typ": *


// Default args as strings containing "//"
#let src = ```
///Description
let func(
link: "//"
)
```.text
#assert.eq(
parse(src).functions,
(
(
name: "func",
description: "Description",
args: (
link: (default: "\"//\"", description: ""),
),
return-types: none
),
)
)
// Check that comments still work
#let src = ```
///Description
let func(
pos, // some comment
)
```.text
#assert.eq(
parse(src).functions,
(
(
name: "func",
description: "Description",
args: (
pos: (description: ""),
),
return-types: none
),
)
)
1 change: 1 addition & 0 deletions tests/new-parser/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#include "argument-description.typ"
#include "argument-type.typ"
#include "currying.typ"
#include "issue_#40.typ"

#set page(width: auto, height: auto, margin: 2pt)

0 comments on commit e752b87

Please sign in to comment.