Skip to content

Commit

Permalink
Improve changelog grammar
Browse files Browse the repository at this point in the history
This PR makes the changelog parser less strict. This includes removing
the requirement for more than one newline after release sections and
allowing entries to be seperated by many newlines.
  • Loading branch information
Theodus authored and SeanTAllen committed Sep 16, 2017
1 parent 00e0fe4 commit 801af25
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 74 deletions.
76 changes: 39 additions & 37 deletions changelog.pony
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@ class Changelog
unreleased = None
end

fun ref create_release(version: String, date: String) ? =>
fun ref create_release(version: String, date: String) =>
match unreleased
| let r: Release =>
r.heading = "## [" + version + "] - " + date

if (r.fixed as Section).entries == "" then
r.fixed = None
end
if (r.added as Section).entries == "" then
r.added = None
end
if (r.changed as Section).entries == "" then
r.changed = None
end
else
this
end

fun ref create_unreleased() =>
Expand All @@ -58,53 +46,67 @@ class Release
var added: (Section | None)
var changed: (Section | None)

let _unreleased_heading: String = "## [unreleased] - unreleased"

new create(ast: AST) ? =>
let t = ast.children(0)? as Token
heading = t.source.content.trim(t.offset, t.offset + t.length)
heading = (ast.children(0)? as Token).string()
fixed = try Section(ast.children(1)? as AST)? else None end
added = try Section(ast.children(2)? as AST)? else None end
changed = try Section(ast.children(3)? as AST)? else None end

new _unreleased() =>
heading = "## [unreleased] - unreleased"
fixed = Section._emtpy(Fixed)
added = Section._emtpy(Added)
changed = Section._emtpy(Changed)
heading = _unreleased_heading
fixed = Section._empty(Fixed)
added = Section._empty(Added)
changed = Section._empty(Changed)

fun string(): String iso^ =>
let str = recover String .> append(heading) .> append("\n\n") end
for section in [fixed; added; changed].values() do
match section
| let s: this->Section =>
str .> append(s.string()) .> append("\n\n")
if heading == _unreleased_heading then
"\n\n".join(
[ heading
"### Fixed\n"
"### Added\n"
"### Changed\n"
""
].values())
else
let str = recover String .> append(heading) .> append("\n\n") end
for section in [fixed; added; changed].values() do
match section
| let s: Section box =>
str .> append(s.string()) .> append("\n")
end
end
str
end
str

class Section
let label: TSection
let entries: String
embed entries: Array[String]

new create(ast: AST) ? =>
label = (ast.children(0)? as Token).label() as TSection
entries =
try
let t = ast.children(1)? as Token
t.source.content.trim(t.offset, t.offset + t.length)
else
""
end
let es = ast.children(1)? as AST
entries = Array[String](es.size())

new _emtpy(label': TSection) =>
(label, entries) = (label', "")
for entry in es.children.values() do
try entries.push((entry as Token).string()) end
end

new _empty(label': TSection) =>
(label, entries) = (label', Array[String])

fun is_empty(): Bool => entries == ""
fun is_empty(): Bool => entries.size() == 0

fun string(): String =>
let entries' = recover String end
for entry in entries.values() do
entries'.append(entry)
end
recover
String
.> append("### ")
.> append(label.text())
.> append("\n\n")
.> append(entries)
.> append(consume entries')
end
18 changes: 10 additions & 8 deletions changelog_parser.pony
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ primitive ChangelogParser

fun head(): Parser val =>
recover
(not L("\n## [") * Unicode).many().term(THeading)
(not L("\n## [") * Unicode).many().term()
* -L("\n").opt()
end

Expand All @@ -32,9 +32,10 @@ primitive ChangelogParser
let heading = (L("### ") * L(s.text())).term(s)
let entries' =
if released then entries()
else entries().opt() // allow empty sections in unreleased
// allow empty sections in unreleased
else entries().opt()
end
heading * -L("\n\n") * entries' * -L("\n").many1()
(heading * -L("\n").many() * entries').node(NoLabel)
end

fun version(): Parser val =>
Expand All @@ -50,19 +51,19 @@ primitive ChangelogParser
(digits4 * L("-") * digits2 * L("-") * digits2).term(TDate)
end

// TODO parse entries individually
fun entries(): Parser val =>
recover
let chars = R(' ').many1()
(L("- ") * chars * (L("\n") * chars).many()).term(TEntries)
let line = Forward
line() = L("\n") / (Unicode * line)
let sep = L("-") / L("#") / L("\n")
let entry = L("- ") * line * (not sep * line).many()
(entry.term(TEntry) * -L("\n").many()).many1().node(TEntries)
end

fun digits(): Parser val => recover digit().many1() end

fun digit(): Parser val => recover R('0', '9') end

primitive THeading is Label fun text(): String => "Heading"

trait val TSection is Label
primitive Fixed is TSection fun text(): String => "Fixed"
primitive Added is TSection fun text(): String => "Added"
Expand All @@ -72,3 +73,4 @@ primitive TRelease is Label fun text(): String => "Release"
primitive TVersion is Label fun text(): String => "Version"
primitive TDate is Label fun text(): String => "Date"
primitive TEntries is Label fun text(): String => "Entries"
primitive TEntry is Label fun text(): String => "Entry"
11 changes: 9 additions & 2 deletions changelog_tool.pony
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ class ChangelogTool
fun verify() =>
_env.out.print("verifying " + _filename + "...")
try
_parse()?
let ast = _parse()?

// TODO:
// let changelog = Changelog(ast)?
// _env.out.print(changelog.string())
// try changelog .> create_release("0.0.0", "0000-00-00")
// else _env.out.print("fail.")
// end
_env.out.print(_filename + " is a valid changelog")
end

Expand All @@ -23,7 +30,7 @@ class ChangelogTool
let date = Date(Time.seconds()).format("%Y-%m-%d")
let changelog: String =
Changelog(_parse()?)?
.> create_release(version, date)?
.> create_release(version, date)
.string()
_edit_or_print(edit, changelog)
else
Expand Down
3 changes: 0 additions & 3 deletions tests/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ All notable changes to the Pony compiler and standard library will be documented
### Fixed



### Added



### Changed



## [0.13.0] - 2017-04-07

### Fixed
Expand Down
Loading

0 comments on commit 801af25

Please sign in to comment.