Skip to content

Commit

Permalink
Tsify llvm-ast (#4614)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Jan 24, 2023
1 parent 526ea95 commit 2dc004d
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions lib/llvm-ast.js → lib/llvm-ast.ts
Expand Up @@ -22,24 +22,36 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import {CompilationResult} from '../types/compilation/compilation.interfaces';
import {ResultLine} from '../types/resultline/resultline.interfaces';

import {PropertyGetter} from './properties.interfaces';

type Point = {
line: number | null;
col: number | null;
};

export class LlvmAstParser {
constructor(compilerProps) {
maxAstLines: number;

// Almost every line of AST includes a span of related source lines:
// In different forms like <line:a:b, line:c:d>
static readonly locTypes = {
NONE: 'none', // No location specified
POINT: 'point', // A single location: beginning of a token
SPAN: 'span', // Two locations: first token to last token (beginning)
} as const;

constructor(compilerProps: PropertyGetter) {
this.maxAstLines = 500;
if (compilerProps) {
this.maxAstLines = compilerProps('maxLinesOfAst', this.maxAstLines);
}

// Almost every line of AST includes a span of related source lines:
// In different forms like <line:a:b, line:c:d>
this.locTypes = {
NONE: 'none', // No location specified
POINT: 'point', // A single location: beginning of a token
SPAN: 'span', // Two locations: first token to last token (beginning)
};
}

// Accepts "line:a:b" and "col:b"
parsePoint(ptLine, lastLineNo) {
parsePoint(ptLine: string, lastLineNo: number | null): Point {
const lineRegex = /line:(\d+):/;
const colRegex = /(?:col|\d):(\d+)(?::|$)/;
const lineMatch = ptLine.match(lineRegex);
Expand All @@ -53,7 +65,13 @@ export class LlvmAstParser {
// X can be "col:a" or "line:a:b"
// lastLineNo - the line number of the previous node,
// reused when only a column specified.
parseSpan(line, lastLineNo) {
parseSpan(
line: string,
lastLineNo: number | null,
):
| {type: typeof LlvmAstParser.locTypes.SPAN; begin: Point; end: Point}
| {type: typeof LlvmAstParser.locTypes.POINT; loc: Point}
| {type: typeof LlvmAstParser.locTypes.NONE} {
const spanRegex = /<((?:line|col)[\d ,:ceilno]+)>/;
const m = line.match(spanRegex);
if (m) {
Expand All @@ -62,43 +80,44 @@ export class LlvmAstParser {
if (beginEnd.length === 2) {
const begin = this.parsePoint(beginEnd[0], lastLineNo);
const end = this.parsePoint(beginEnd[1], begin.line);
return {type: this.locTypes.SPAN, begin, end};
return {type: LlvmAstParser.locTypes.SPAN, begin, end};
} else {
return {type: this.locTypes.POINT, loc: this.parsePoint(span, lastLineNo)};
return {type: LlvmAstParser.locTypes.POINT, loc: this.parsePoint(span, lastLineNo)};
}
}
return {type: this.locTypes.NONE};
return {type: LlvmAstParser.locTypes.NONE};
}

// Link the AST lines with spans of source locations (lines+columns)
parseAndSetSourceLines(astDump) {
let lfrom = {line: null, loc: null},
lto = {line: null, loc: null};
parseAndSetSourceLines(astDump: ResultLine[]) {
let lfrom: any = {line: null, loc: null},
lto: any = {line: null, loc: null};
for (const line of astDump) {
const span = this.parseSpan(line.text, lfrom.line);
switch (span.type) {
case this.locTypes.NONE: {
case LlvmAstParser.locTypes.NONE: {
break;
}
case this.locTypes.POINT: {
case LlvmAstParser.locTypes.POINT: {
lfrom = span.loc;
lto = span.loc;
break;
}
case this.locTypes.SPAN: {
case LlvmAstParser.locTypes.SPAN: {
lfrom = span.begin;
lto = span.end;
break;
}
}
if (span.type !== this.locTypes.NONE) {
line.source = {from: lfrom, to: lto};
if (span.type !== LlvmAstParser.locTypes.NONE) {
// TODO: ResultLineSource doesn't have to/from
(line.source as any) = {from: lfrom, to: lto};
}
}
}

processAst(output) {
output = output.stdout;
processAst(result: CompilationResult) {
const output = result.stdout;

// Top level decls start with |- or `-
const topLevelRegex = /^([`|])-/;
Expand Down

0 comments on commit 2dc004d

Please sign in to comment.