Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser multiline comment #321

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test:typeSystem": "mocha --parallel -r ts-node/register/transpile-only test/typeSystem*.test.ts",
"test:wtest": "mocha --delay -t 10000 -r ts-node/register/transpile-only test/wtest.ts",
"test:printer": "mocha --parallel -r ts-node/register/transpile-only test/printer.test.ts",
"test:parser": "mocha --parallel -r ts-node/register/transpile-only test/parser.test.ts",
"test:parser": "mocha -r ts-node/register/transpile-only test/parser.test.ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esto quedó sin el parallel, ups

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creo que no importa, porque no se llama cuando se corren todos los tests.

De hecho, yo propongo tener:

  • todos los tests particulares (parser, printer, etc) sin el parallel
  • tener un all o unit que haga mocha -r ts-node/register/transpile-only test/**/*.ts para que corra todo sin importar

Ahora ya tenemos todo esto, pero no está bien organizado.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh ahí me fijé y ya está así el unit. Solamente hay cosas de más en el script de npm test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no entendí muy bien, pero me parece que el parallel tiene sentido si querés correr solo los tests del parser

"lint:fix": "eslint . --fix",
"validate:wollokVersion": "ts-node scripts/validateWollokVersion.ts",
"prepublishOnly": "npm run validate:wollokVersion && npm run build && npm test",
Expand Down
8 changes: 5 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,24 @@ const lastKey = (str: string) => _.then(string(str))

const _ = optional(whitespace.atLeast(1))
const __ = optional(key(';').or(Parsimmon.regex(/\s/)))
const spaces = optional(string(' ').many())

const comment = (position: 'start' | 'end' | 'inner') => lazy('comment', () => regex(/\/\*(.|[\r\n])*?\*\/|\/\/.*/)).map(text => new Annotation('comment', { text, position }))
const sameLineComment: Parser<Annotation> = comment('end')
const sameLineComment: Parser<Annotation> = spaces.then(comment('end'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magia pura acá, por suerte mientras lo paireamos lo ibas explicando


export const sanitizeWhitespaces = (originalFrom: SourceIndex, originalTo: SourceIndex, input: string): [SourceIndex, SourceIndex] => {
const EOL = input.includes('\r\n') ? '\r\n' : '\n'
const hasLineBreaks = (aString: string) => aString.includes(EOL)
const nodeInput = input.substring(originalFrom.offset, originalTo.offset)
const hasWhitespaceAtTheEnd = hasWhitespace(input[originalTo.offset - 1])
const hasWhitespaceAtTheEnd = hasWhitespace(input[originalTo.offset])
const shouldBeSanitized = hasWhitespace(nodeInput) || hasWhitespaceAtTheEnd || hasWhitespace(input[originalFrom.offset])
if (!shouldBeSanitized) return [originalFrom, originalTo]
const from = { ...originalFrom }
const to = { ...originalTo }

// Fix incorrect offset / column-line border case
if (hasWhitespace(input[to.offset]) && to.column == 0) { to.offset++ }
if (hasWhitespace(input[from.offset]) && from.column == 0) { from.offset++ }

while (hasWhitespace(input[from.offset]) && from.offset < originalTo.offset) {
if (hasLineBreaks(input.substring(from.offset, from.offset + EOL.length))) {
Expand Down Expand Up @@ -254,7 +256,7 @@ const parameters: Parser<List<ParameterNode>> = lazy(() =>
Parameter.sepBy(key(',')).wrap(key('('), lastKey(')')))

const unamedArguments: Parser<List<ExpressionNode>> = lazy(() =>
Expression.sepBy(key(',')).wrap(key('('), key(')')))
Expression.sepBy(key(',')).wrap(key('('), lastKey(')')))

const namedArguments: Parser<List<NamedArgumentNode>> = lazy(() =>
NamedArgument.sepBy(key(',')).wrap(key('('), lastKey(')'))
Expand Down
53 changes: 48 additions & 5 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { should, use } from 'chai'
import { LIST_MODULE, SET_MODULE } from '../src'
import { Annotation, Assignment, Body, Catch, Class, Closure, Describe, Field, If, Import, Literal, Method, Mixin, NamedArgument, New, Package, Parameter, ParameterizedType, Program, Reference, Return, Send, Singleton, SourceIndex, Super, Test, Throw, Try, Variable } from '../src/model'
import { Annotation, Assignment, Body, Catch, Class, Closure, Describe, Field, If, Import, Literal, Method, Mixin, NamedArgument, New, Package, Parameter, ParameterizedType, Program, Reference, Return, Self, Send, Singleton, SourceIndex, Super, Test, Throw, Try, Variable } from '../src/model'
import * as parse from '../src/parser'
import { parserAssertions } from './assertions'

Expand Down Expand Up @@ -50,8 +50,10 @@ describe('Wollok parser', () => {
.should.be.parsedBy(parse.Variable).into(new Variable({
name: 'a',
isConstant: true,
value: new Literal({ value: 1 }),
metadata: [new Annotation('comment', { text: '//some comment', position: 'end' })],
value: new Literal({
value: 1,
metadata: [new Annotation('comment', { text: '//some comment', position: 'end' })],
}),
}))
})

Expand All @@ -63,8 +65,10 @@ describe('Wollok parser', () => {
new Variable({
name: 'a',
isConstant: true,
value: new Literal({ value: 1 }),
metadata: [new Annotation('comment', { text: '//some comment', position: 'end' })],
value: new Literal({
value: 1,
metadata: [new Annotation('comment', { text: '//some comment', position: 'end' })],
}),
}),
],
}))
Expand Down Expand Up @@ -234,6 +238,45 @@ describe('Wollok parser', () => {
.and.be.tracedTo(0, 124)
})

it('comments before many members with body expressions with send', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏼 👏🏼 👏🏼

`class c {
//some comment
method m1() = self.m2()

//other comment
method m2() = 2
}`.should.be.parsedBy(parser).into(new Class({
name: 'c',
metadata: [],
members: [
new Method({
name: 'm1',
body: new Body({
sentences: [
new Return({
value: new Send({
receiver: new Self(),
message: 'm2',
}),
}),
],
}),
metadata: [
new Annotation('comment', { text: '//some comment', position: 'start' }),
],
}),
new Method({
name: 'm2',
body: new Body({ sentences: [new Return({ value: new Literal({ value: 2 }) })] }),
metadata: [
new Annotation('comment', { text: '//other comment', position: 'start' }),
],
}),
],
}))
.and.be.tracedTo(0, 132)
})

it('comments with block closures', () => {
`class c {
//some comment
Expand Down
23 changes: 21 additions & 2 deletions test/printer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ describe('Wollok Printer', () => {
// comentario
const a = 1
// other comment but this comment is actually very veeeeeeeeeeeeery veeeeeeeeeery long
const b = 2
// last comentario
const b = 2 // last comentario
}`)
})

Expand Down Expand Up @@ -989,6 +988,7 @@ describe('Wollok Printer', () => {

console.println("")
console.println("")

}
}`.should.be.formattedTo( `
object foo {
Expand Down Expand Up @@ -1277,6 +1277,25 @@ describe('Wollok Printer', () => {
self.println(a + b)
}`)
})

it('testSimpleProgramWithSpacesBetweenSends', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qué beio!!!

`program p {
self.println(1)
self.println(2)

self.println(3)

self.println(4)
}`.should.be.formattedTo(`
program p {
self.println(1)
self.println(2)

self.println(3)

self.println(4)
}`)
})
})
describe('Testing', () => {
it('testConstantsFormatting', () => {
Expand Down
Loading