Skip to content

Commit

Permalink
🥅 Add parse error for line continuation at end of file
Browse files Browse the repository at this point in the history
- it doesn't count as end of file iff there is any content after the newline after the backslash (including spaces), so catch that in an explicit case
  • Loading branch information
TheAfroOfDoom committed May 24, 2024
1 parent d3705e0 commit f6c0638
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
76 changes: 76 additions & 0 deletions __snapshots__/packages/core/test-out/parser/util.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion packages/core/src/parser/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AstNode } from '../node/index.js'
import { localize } from '@spyglassmc/locales'
import type { AstNode, ErrorNode } from '../node/index.js'
import { SequenceUtil, SequenceUtilDiscriminator } from '../node/index.js'
import type { ParserContext } from '../service/index.js'
import { ErrorReporter } from '../service/index.js'
Expand Down Expand Up @@ -544,6 +545,20 @@ export function concatOnTrailingBackslash<N extends Returnable>(
// next line's first non-whitespace character
const from = src.getCharRange()
src.nextLine()

// Minecraft raises a `Line continuation at end of file` if a backslash
// (+ optional whitespace to the next line) is right before the end of the file
if (!src.canRead()) {
const ans: ErrorNode = {
type: 'error',
range: Range.span(from, src),
}
ctx.err.report(
localize('mcfunction.parser.line-continuation-end-of-file'),
ans,
)
}

src.skipSpace()
const to = src.getCharRange(-1)
indexMap.push({
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/parser/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ describe('concatOnTrailingBackslash()', () => {
{ content: 'tru\\ \n \\\n e' },
{ content: 'tru\\e \\ \n e' },
{ content: 'tru\\\n\ne' },
{ content: 'tru\\' },
{ content: 'tru\\ \n' },
{ content: 'tru\\ \n ' },
],
},
]
Expand Down
1 change: 1 addition & 0 deletions packages/locales/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"mcfunction.parser.entity-selector.player-name.too-long": "Player names cannot be longer than %0% characters",
"mcfunction.parser.eoc-unexpected": "Expected more arguments",
"mcfunction.parser.leading-slash": "a leading slash %0%",
"mcfunction.parser.line-continuation-end-of-file": "A line continuation cannot be the end of the file",
"mcfunction.parser.no-permission": "Permission level %0% is required, which is higher than %1% defined in config",
"mcfunction.parser.objective.too-long": "Objective names cannot be longer than %0% characters",
"mcfunction.parser.range.min>max": "The minimum value %0% is larger than the maximum value %1%",
Expand Down

0 comments on commit f6c0638

Please sign in to comment.