Skip to content

Commit

Permalink
1/5 tree walkers done (GrammarTreeVisitor)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-lischke committed Oct 13, 2024
1 parent 7ae812c commit 41e0163
Show file tree
Hide file tree
Showing 6 changed files with 719 additions and 586 deletions.
25 changes: 13 additions & 12 deletions src/antlr3/BaseRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,19 +665,20 @@ export abstract class BaseRecognizer {
const followSet = new BitSet();
for (let i = top; i >= 0; i--) {
const localFollowSet = this.state.following[i];
followSet.or(localFollowSet);
if (exact) {
// can we see end of rule?
if (localFollowSet.get(TreeParser.EOR_TOKEN_TYPE)) {
// Only leave EOR in set if at top (start rule); this lets
// us know if have to include follow(start rule); i.e., EOF
if (i > 0) {
followSet.clear(TreeParser.EOR_TOKEN_TYPE);
if (localFollowSet) {
followSet.or(localFollowSet);
if (exact) {
// can we see end of rule?
if (localFollowSet.get(TreeParser.EOR_TOKEN_TYPE)) {
// Only leave EOR in set if at top (start rule); this lets
// us know if have to include follow(start rule); i.e., EOF
if (i > 0) {
followSet.clear(TreeParser.EOR_TOKEN_TYPE);
}
} else { // can't see end of rule, quit
break;
}
}
else { // can't see end of rule, quit
break;
}
}
}

Expand Down Expand Up @@ -797,7 +798,7 @@ export abstract class BaseRecognizer {
}

/** Push a rule's follow set using our own hardcoded stack */
protected pushFollow(fset: BitSet): void {
protected pushFollow(fset: BitSet | null): void {
if ((this.state._fsp + 1) >= this.state.following.length) {
this.state.following = this.state.following.slice();
}
Expand Down
39 changes: 39 additions & 0 deletions src/antlr3/EarlyExitException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
[The "BSD license"]
Copyright (c) 2005-2009 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { RecognitionException, type IntStream } from "antlr4ng";

/** The recognizer did not match anything for a (..)+ loop. */
export class EarlyExitException extends RecognitionException {
public decisionNumber: number;

public constructor(decisionNumber: number, input?: IntStream) {
super({ message: `@[${decisionNumber}]`, recognizer: null, input: null, ctx: null });
this.decisionNumber = decisionNumber;
}
}
38 changes: 38 additions & 0 deletions src/antlr3/MismatchedSetException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
[The "BSD license"]
Copyright (c) 2005-2009 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { RecognitionException, type BitSet, type IntStream } from "antlr4ng";

export class MismatchedSetException extends RecognitionException {
public expecting: BitSet | null;

public constructor(expecting: BitSet | null, input?: IntStream) {
super({ message: "", recognizer: null, input: null, ctx: null });
this.expecting = expecting;
}
}
43 changes: 43 additions & 0 deletions src/antlr3/NoViableAltException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
[The "BSD license"]
Copyright (c) 2005-2009 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { RecognitionException, type IntStream } from "antlr4ng";

export class NoViableAltException extends RecognitionException {
public grammarDecisionDescription: string;
public decisionNumber: number;
public stateNumber: number;

public constructor(grammarDecisionDescription: string, decisionNumber: number, stateNumber: number,
input?: IntStream) {
super({ message: `@[${grammarDecisionDescription}]`, recognizer: null, input: null, ctx: null });
this.grammarDecisionDescription = grammarDecisionDescription;
this.decisionNumber = decisionNumber;
this.stateNumber = stateNumber;
}
}
2 changes: 1 addition & 1 deletion src/antlr3/RecognizerSharedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class RecognizerSharedState {
* Stack grows upwards. When it hits the max, it grows 2x in size
* and keeps going.
*/
public following = new Array<BitSet>(BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE);
public following = new Array<BitSet | null>(BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE);
public _fsp = -1;

/**
Expand Down
Loading

0 comments on commit 41e0163

Please sign in to comment.