Skip to content

Commit c9caa51

Browse files
authored
Separated the Lisp Container objects from the Lisp Formatting objects (#95)
Separated the Lisp Container objects from the Lisp Formatting objects (#95)
1 parent 2d7759d commit c9caa51

File tree

12 files changed

+937
-455
lines changed

12 files changed

+937
-455
lines changed

extension/src/format/parser.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import { ListReader, CursorPosition } from "./listreader";
3-
import { LispAtom, Sexpression } from "./sexpression";
3+
import { LispAtom, Sexpression, LispContainer } from "./sexpression";
44

55
class LeftParentItem {
66
public location: Number;
@@ -202,15 +202,17 @@ export namespace LispParser {
202202
linefeed: string;
203203
}
204204

205-
export function getDocumentSexpression(data: string|vscode.TextDocument, offset?: number|vscode.Position, tracker?: CountTracker): Sexpression {
205+
export function getDocumentContainer(data: string|vscode.TextDocument, offset?: number|vscode.Position, tracker?: CountTracker): LispContainer {
206206
let documentText = '';
207+
let isTopLevel = false;
207208
if (data instanceof Object) {
208209
documentText = data.getText();
209210
} else {
210211
documentText = data;
211212
}
212-
if (!offset) {
213+
if (offset === undefined) {
213214
offset = 0;
215+
isTopLevel = true;
214216
}
215217
if (!tracker){
216218
tracker = {
@@ -219,7 +221,7 @@ export namespace LispParser {
219221
};
220222
}
221223

222-
const result = new Sexpression();
224+
const result = new LispContainer();
223225
result.line = tracker.line;
224226
result.column = tracker.column;
225227
result.linefeed = tracker.linefeed;
@@ -247,8 +249,15 @@ export namespace LispParser {
247249
}
248250
tracker.idx++;
249251
tracker.column++;
252+
} else if (isTopLevel && curr === '(' && state === ParseState.UNKNOWN) {
253+
if (temp.length > 0) {
254+
result.atoms.push(new LispAtom(grpStart.line, grpStart.character, temp));
255+
}
256+
temp = '';
257+
grpStart = null;
258+
result.atoms.push(getDocumentContainer(documentText, offset, tracker));
250259
} else {
251-
let handled = false; // Only true when this function gets recursively called because of a new open parenthesis (Sexpression) scope
260+
let handled = false; // Only true when this function gets recursively called because of a new open parenthesis (LispContainer) scope
252261
switch (state) {
253262
case ParseState.UNKNOWN:
254263
if (grpStart === null && curr === '\'' && next === '(') {
@@ -269,7 +278,7 @@ export namespace LispParser {
269278
result.atoms.push(new LispAtom(grpStart.line, grpStart.character, temp));
270279
}
271280
temp = '';
272-
result.atoms.push(getDocumentSexpression(documentText, offset, tracker));
281+
result.atoms.push(getDocumentContainer(documentText, offset, tracker));
273282
handled = true;
274283
} else if (curr === ';') {
275284
if (temp.length > 0) {
@@ -328,7 +337,9 @@ export namespace LispParser {
328337
}
329338
} else {
330339
if (curr === '\r' || curr === '\n') {
331-
result.atoms.push(new LispAtom(grpStart.line, grpStart.character, temp));
340+
if (temp !== '') {
341+
result.atoms.push(new LispAtom(grpStart.line, grpStart.character, temp));
342+
}
332343
state = ParseState.UNKNOWN;
333344
temp = '';
334345
} else {

0 commit comments

Comments
 (0)