Skip to content

Commit

Permalink
Some types fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
barsdeveloper committed Jan 24, 2024
1 parent 674093c commit 4c59cb0
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 101 deletions.
60 changes: 20 additions & 40 deletions dist/parsernostrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Reply {
}
}

/** @param {Parsernostrum<Parser<any>>} parsernostrum */
/** @param {Parsernostrum<Parser>} parsernostrum */
static makeContext(parsernostrum = null, input = "") {
return /** @type {Context} */({
parsernostrum,
Expand All @@ -49,7 +49,6 @@ class Reply {
}
}

/** @template T */
class Parser {

static indentation = " "
Expand Down Expand Up @@ -149,7 +148,7 @@ class Parser {
* @param {Context} context
* @param {Number} position
* @param {PathNode} path
* @returns {Result<T>}
* @returns {Result<ParserValue<any>>}
*/
parse(context, position, path) {
return null
Expand All @@ -175,10 +174,7 @@ class Parser {
}
}

/**
* @template {String} T
* @extends {Parser<T>}
*/
/** @template {String} T */
class StringParser extends Parser {

#value
Expand Down Expand Up @@ -224,7 +220,6 @@ class StringParser extends Parser {
}
}

/** @extends Parser<String> */
class SuccessParser extends Parser {

static instance = new SuccessParser()
Expand Down Expand Up @@ -253,10 +248,7 @@ class SuccessParser extends Parser {
}
}

/**
* @template {Parser<any>[]} T
* @extends Parser<ParserValue<T>>
*/
/** @template {Parser[]} T */
class AlternativeParser extends Parser {

static highlightRegexp = new RegExp(
Expand Down Expand Up @@ -340,9 +332,8 @@ class AlternativeParser extends Parser {
}

/**
* @template {Parser<any>} T
* @template {(v: ParserValue<T>, input: String, position: Number) => Parsernostrum<Parser<any>>} C
* @extends Parser<ReturnType<C>>
* @template {Parser} T
* @template {(v: ParserValue<T>, input: String, position: Number) => Parsernostrum<Parser>} C
*/
class ChainedParser extends Parser {

Expand All @@ -367,12 +358,15 @@ class ChainedParser extends Parser {
* @param {Context} context
* @param {Number} position
* @param {PathNode} path
* @returns {Result<ParserValue<UnwrapParser<ReturnType<C>>>>}
*/
parse(context, position, path) {
const outcome = this.#parser.parse(context, position, { parent: path, parser: this.#parser, index: 0 });
if (!outcome.status) {
// @ts-expect-error
return outcome
}
// @ts-expect-error
const result = this.#fn(outcome.value, context.input, outcome.position)
.getParser()
.parse(context, outcome.position);
Expand Down Expand Up @@ -410,7 +404,6 @@ class ChainedParser extends Parser {
}
}

/** @extends Parser<any> */
class FailureParser extends Parser {

static instance = new FailureParser()
Expand Down Expand Up @@ -440,10 +433,7 @@ class FailureParser extends Parser {
}
}

/**
* @template {Parser<any>} T
* @extends Parser<ParserValue<T>>
*/
/** @template {Parser} T */
class LazyParser extends Parser {

#parser
Expand Down Expand Up @@ -489,7 +479,7 @@ class LazyParser extends Parser {
}
}

/** @template {Parser<any>} T */
/** @template {Parser} T */
class Lookahead extends Parser {

#parser
Expand Down Expand Up @@ -562,10 +552,7 @@ class Lookahead extends Parser {
}
}

/**
* @template T
* @extends {Parser<T>}
*/
/** @template T */
class RegExpParser extends Parser {

/** @type {RegExp} */
Expand Down Expand Up @@ -646,9 +633,8 @@ class RegExpParser extends Parser {
}

/**
* @template {Parser<any>} T
* @template {Parser} T
* @template P
* @extends Parser<P>
*/
class MapParser extends Parser {

Expand Down Expand Up @@ -744,10 +730,7 @@ class RegExpValueParser extends RegExpParser {
}
}

/**
* @template {Parser<any>[]} T
* @extends Parser<ParserValue<T>>
*/
/** @template {Parser[]} T */
class SequenceParser extends Parser {

#parsers
Expand Down Expand Up @@ -809,10 +792,7 @@ class SequenceParser extends Parser {
}
}

/**
* @template {Parser<any>} T
* @extends {Parser<ParserValue<T>[]>}
*/
/** @template {Parser} T */
class TimesParser extends Parser {

#parser
Expand Down Expand Up @@ -903,12 +883,12 @@ class TimesParser extends Parser {
}
}

/** @template {Parser<any>} T */
/** @template {Parser} T */
class Parsernostrum {

#parser

/** @type {(new (parser: Parser<any>) => Parsernostrum<typeof parser>) & typeof Parsernostrum} */
/** @type {(new (parser: Parser) => Parsernostrum<typeof parser>) & typeof Parsernostrum} */
Self

static lineColumnFromOffset(string, offset) {
Expand Down Expand Up @@ -1149,7 +1129,7 @@ class Parsernostrum {
}

/**
* @template {Parsernostrum<Parser<any>>} P
* @template {Parsernostrum<Parser>} P
* @param {P} separator
*/
sepBy(separator, allowTrailing = false) {
Expand All @@ -1176,7 +1156,7 @@ class Parsernostrum {
}

/**
* @template {Parsernostrum<any>} P
* @template {Parsernostrum<Parser>} P
* @param {(v: ParserValue<T>, input: String, position: Number) => P} fn
*/
chain(fn) {
Expand All @@ -1199,7 +1179,7 @@ class Parsernostrum {
return this.map(Parsernostrum.#joiner)
}

/** @param {Parsernostrum<Parser<any>> | Parser<any> | PathNode} highlight */
/** @param {Parsernostrum<Parser> | Parser | PathNode} highlight */
toString(indent = 0, newline = false, highlight = null) {
if (highlight instanceof Parsernostrum) {
highlight = highlight.getParser();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "parsernostrum",
"type": "module",
"version": "1.1.1",
"version": "1.1.2",
"description": "A tiny LL parser combinator library for javascript",
"main": "src/Parsernostrum.js",
"types": "src/types.js",
Expand Down
10 changes: 5 additions & 5 deletions src/Parsernostrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import StringParser from "./parser/StringParser.js"
import SuccessParser from "./parser/SuccessParser.js"
import TimesParser from "./parser/TimesParser.js"

/** @template {Parser<any>} T */
/** @template {Parser} T */
export default class Parsernostrum {

#parser

/** @type {(new (parser: Parser<any>) => Parsernostrum<typeof parser>) & typeof Parsernostrum} */
/** @type {(new (parser: Parser) => Parsernostrum<typeof parser>) & typeof Parsernostrum} */
Self

static lineColumnFromOffset(string, offset) {
Expand Down Expand Up @@ -260,7 +260,7 @@ export default class Parsernostrum {
}

/**
* @template {Parsernostrum<Parser<any>>} P
* @template {Parsernostrum<Parser>} P
* @param {P} separator
*/
sepBy(separator, allowTrailing = false) {
Expand All @@ -287,7 +287,7 @@ export default class Parsernostrum {
}

/**
* @template {Parsernostrum<any>} P
* @template {Parsernostrum<Parser>} P
* @param {(v: ParserValue<T>, input: String, position: Number) => P} fn
*/
chain(fn) {
Expand All @@ -310,7 +310,7 @@ export default class Parsernostrum {
return this.map(Parsernostrum.#joiner)
}

/** @param {Parsernostrum<Parser<any>> | Parser<any> | PathNode} highlight */
/** @param {Parsernostrum<Parser> | Parser | PathNode} highlight */
toString(indent = 0, newline = false, highlight = null) {
if (highlight instanceof Parsernostrum) {
highlight = highlight.getParser()
Expand Down
2 changes: 1 addition & 1 deletion src/Reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class Reply {
}
}

/** @param {Parsernostrum<Parser<any>>} parsernostrum */
/** @param {Parsernostrum<Parser>} parsernostrum */
static makeContext(parsernostrum = null, input = "") {
return /** @type {Context} */({
parsernostrum,
Expand Down
4 changes: 2 additions & 2 deletions src/grammars/JsonGrammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export default class JsonGrammar {
static #false = P.str("false").map(() => false)
static #string = P.doubleQuotedString
static #number = P.numberExponential
/** @type {Parsernostrum<Parser<any[]>>} */
/** @type {Parsernostrum<Parser>} */
static #array = P.seq(
P.reg(/\[\s*/),
P.lazy(() => this.json).sepBy(P.reg(/\s*,\s*/)),
P.reg(/\s*\]/)
).map(([_0, values, _2]) => values)
/** @type {Parsernostrum<Parser<Object>>} */
/** @type {Parsernostrum<Parser>} */
static #object = P.seq(
P.reg(/\{\s*/),
P.seq(
Expand Down
5 changes: 1 addition & 4 deletions src/parser/AlternativeParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import Reply from "../Reply.js"
import StringParser from "./StringParser.js"
import SuccessParser from "./SuccessParser.js"

/**
* @template {Parser<any>[]} T
* @extends Parser<ParserValue<T>>
*/
/** @template {Parser[]} T */
export default class AlternativeParser extends Parser {

static highlightRegexp = new RegExp(
Expand Down
9 changes: 5 additions & 4 deletions src/parser/ChainedParser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Parser from "./Parser.js"
import Reply from "../Reply.js"

/**
* @template {Parser<any>} T
* @template {(v: ParserValue<T>, input: String, position: Number) => Parsernostrum<Parser<any>>} C
* @extends Parser<ReturnType<C>>
* @template {Parser} T
* @template {(v: ParserValue<T>, input: String, position: Number) => Parsernostrum<Parser>} C
*/
export default class ChainedParser extends Parser {

Expand All @@ -29,12 +27,15 @@ export default class ChainedParser extends Parser {
* @param {Context} context
* @param {Number} position
* @param {PathNode} path
* @returns {Result<ParserValue<UnwrapParser<ReturnType<C>>>>}
*/
parse(context, position, path) {
const outcome = this.#parser.parse(context, position, { parent: path, parser: this.#parser, index: 0 })
if (!outcome.status) {
// @ts-expect-error
return outcome
}
// @ts-expect-error
const result = this.#fn(outcome.value, context.input, outcome.position)
.getParser()
.parse(context, outcome.position)
Expand Down
1 change: 0 additions & 1 deletion src/parser/FailureParser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Parser from "./Parser.js"
import Reply from "../Reply.js"

/** @extends Parser<any> */
export default class FailureParser extends Parser {

static instance = new FailureParser()
Expand Down
5 changes: 1 addition & 4 deletions src/parser/LazyParser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Parser from "./Parser.js"

/**
* @template {Parser<any>} T
* @extends Parser<ParserValue<T>>
*/
/** @template {Parser} T */
export default class LazyParser extends Parser {

#parser
Expand Down
2 changes: 1 addition & 1 deletion src/parser/Lookahead.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Parser from "./Parser.js"
import Reply from "../Reply.js"

/** @template {Parser<any>} T */
/** @template {Parser} T */
export default class Lookahead extends Parser {

#parser
Expand Down
3 changes: 1 addition & 2 deletions src/parser/MapParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import Parser from "./Parser.js"
import RegExpParser from "./RegExpParser.js"

/**
* @template {Parser<any>} T
* @template {Parser} T
* @template P
* @extends Parser<P>
*/
export default class MapParser extends Parser {

Expand Down
3 changes: 1 addition & 2 deletions src/parser/Parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Reply from "../Reply.js"

/** @template T */
export default class Parser {

static indentation = " "
Expand Down Expand Up @@ -100,7 +99,7 @@ export default class Parser {
* @param {Context} context
* @param {Number} position
* @param {PathNode} path
* @returns {Result<T>}
* @returns {Result<ParserValue<any>>}
*/
parse(context, position, path) {
return null
Expand Down
5 changes: 1 addition & 4 deletions src/parser/RegExpParser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import Parser from "./Parser.js"
import Reply from "../Reply.js"

/**
* @template T
* @extends {Parser<T>}
*/
/** @template T */
export default class RegExpParser extends Parser {

/** @type {RegExp} */
Expand Down
5 changes: 1 addition & 4 deletions src/parser/SequenceParser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import Parser from "./Parser.js"
import Reply from "../Reply.js"

/**
* @template {Parser<any>[]} T
* @extends Parser<ParserValue<T>>
*/
/** @template {Parser[]} T */
export default class SequenceParser extends Parser {

#parsers
Expand Down
Loading

0 comments on commit 4c59cb0

Please sign in to comment.