-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) { | ||
|
@@ -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(')')) | ||
|
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' | ||
|
||
|
@@ -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' })], | ||
}), | ||
})) | ||
}) | ||
|
||
|
@@ -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' })], | ||
}), | ||
}), | ||
], | ||
})) | ||
|
@@ -234,6 +238,45 @@ describe('Wollok parser', () => { | |
.and.be.tracedTo(0, 124) | ||
}) | ||
|
||
it('comments before many members with body expressions with send', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
}`) | ||
}) | ||
|
||
|
@@ -989,6 +988,7 @@ describe('Wollok Printer', () => { | |
|
||
console.println("") | ||
console.println("") | ||
|
||
} | ||
}`.should.be.formattedTo( ` | ||
object foo { | ||
|
@@ -1277,6 +1277,25 @@ describe('Wollok Printer', () => { | |
self.println(a + b) | ||
}`) | ||
}) | ||
|
||
it('testSimpleProgramWithSpacesBetweenSends', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
parallel
all
ounit
que hagamocha -r ts-node/register/transpile-only test/**/*.ts
para que corra todo sin importarAhora ya tenemos todo esto, pero no está bien organizado.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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