Skip to content

Commit 7112569

Browse files
committed
Fix positions and missing file paths
1 parent 77cc0e9 commit 7112569

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/ast.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ export namespace syxparser {
2424
/**
2525
* Parses the token list given into statements and expressions.
2626
* @param {Token[]} t Token list to parse.
27+
* @param {string} _filePath Path of the file that is being parsed.
2728
* @returns Main {@link ProgramStatement} containing all other statements.
2829
* @author efekos
29-
* @version 1.0.2
30+
* @version 1.0.3
3031
* @since 0.0.1-alpha
3132
*/
32-
export function parseTokens(t: Token[],filePath:string): ProgramStatement {
33+
export function parseTokens(t: Token[],_filePath:string): ProgramStatement {
3334
tokens = t;
3435

3536
const eof = t.find(r => r.type === TokenType.EndOfFile);
3637
program = { body: [], type: NodeType.Program, range: { end: eof.range.end, start: { line: 0, character: 0 } } };
37-
this.filePath = filePath;
38+
filePath = _filePath;
3839

3940
while (canGo()) {
4041
parseStatement();
@@ -404,14 +405,15 @@ export namespace sysparser {
404405
* @param {Token[]} t Token list to parse.
405406
* @returns Main {@link ProgramStatement} containing all other statements.
406407
* @author efekos
407-
* @version 1.0.1
408+
* @version 1.0.2
408409
* @since 0.0.1-alpha
409410
*/
410-
export function parseTokens(t: Token[]): ProgramStatement {
411+
export function parseTokens(t: Token[],_filePath:string): ProgramStatement {
411412
tokens = t;
412413

413414
const eof = t.find(r => r.type === TokenType.EndOfFile);
414415
program = { body: [], type: NodeType.Program, range: { start: { character: 0, line: 0 }, end: eof.range.end } };
416+
filePath = _filePath;
415417

416418
while (canGo()) {
417419
parseStatement();

src/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class SyntaxScriptCompiler {
6565
* Compiles one .syx file from the path given.
6666
* @param {string} file Path to a file to compile.
6767
* @author efekos
68-
* @version 1.0.3
68+
* @version 1.0.4
6969
* @since 0.0.1-alpha
7070
*/
7171
public compileSyx(file: string) {
@@ -194,10 +194,10 @@ export class SyntaxScriptCompiler {
194194
* @param {string} file Path to the .sys file to compile.
195195
* @author efekos
196196
* @since 0.0.1-alpha
197-
* @version 1.0.2
197+
* @version 1.0.3
198198
*/
199199
public compileSys(file: string) {
200-
const ast = sysparser.parseTokens(tokenizeSys(readFileSync(file).toString()));
200+
const ast = sysparser.parseTokens(tokenizeSys(readFileSync(file).toString()),file);
201201

202202
//# Handle import statements
203203
var imported: AnyExportable[] = [];

src/diagnostic.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport } from 'lsp-types';
1+
import { CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport,Range } from 'lsp-types';
22
import { sysparser, syxparser } from './ast.js';
33
import { tokenizeSys, tokenizeSyx } from './lexer.js';
44
import { isCompilerError } from './types.js';
@@ -27,7 +27,7 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent
2727
if (isCompilerError(error)) {
2828
items.push({
2929
message: error.message,
30-
range: error.range,
30+
range: subRange(error.range),
3131
severity: DiagnosticSeverity.Error,
3232
source: 'syntax-script',
3333
data: [
@@ -38,7 +38,7 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent
3838
changes: {
3939
[filePath]: [
4040
{
41-
range:error.range,
41+
range:subRange(error.range),
4242
newText:'test'
4343
}
4444
]
@@ -54,3 +54,13 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent
5454
}
5555

5656
}
57+
58+
59+
/**
60+
* Modifies the given range to be zero-based.
61+
* @param {Range} r Any range.
62+
* @returns Same range with every value decreased by 1.
63+
*/
64+
function subRange(r:Range):Range {
65+
return {start:{character:r.start.character-1,line:r.start.line-1},end:{character:r.end.character-1,line:r.end.line-1}};
66+
}

src/lexer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ function tpr(start: Position, end: Position): Range {
6969
* @param {string} source Source string.
7070
* @returns A list of tokens generated from source string.
7171
* @author efekos
72-
* @version 1.0.6
72+
* @version 1.0.7
7373
* @since 0.0.1-alpha
7474
* @throws LexerError if an error occurs.
7575
*/
7676
export function tokenizeSyx(source: string): Token[] {
7777
const tokens: Token[] = [];
7878
const src = source.split('');
7979
let curPos = 0;
80-
let curLine = 0;
80+
let curLine = 1;
8181

8282
while (src.length > 0) {
8383
if (src[0] === '/' && src[1] === '/') {
@@ -138,15 +138,15 @@ export function tokenizeSyx(source: string): Token[] {
138138
* @param {string} source Source string.
139139
* @returns A list of tokens generated from the source file.
140140
* @author efekos
141-
* @version 1.0.3
141+
* @version 1.0.4
142142
* @since 0.0.1-alpha
143143
*/
144144
export function tokenizeSys(source: string): Token[] {
145145
const src = source.split('');
146146
const tokens: Token[] = [];
147147

148148
let curPos = 0;
149-
let curLine = 0;
149+
let curLine = 1;
150150

151151
while (src.length > 0 && `${src[0]}${src[1]}${src[2]}` !== ':::') {
152152
if (src[0] === ';') tokens.push({ type: TokenType.Semicolon, value: src.shift(), range: opr(curLine, curPos++) });

0 commit comments

Comments
 (0)