kwasm.format.text

Kwasm.format.text

package kwasm.format.text

Classes

Name Description
data class TextModuleCounts
data class ParseResult
class Tokenizer

A tokenizer capable of splitting a raw text-format WASM file into its component tokens.

From the docs:

The character stream in the source text is divided, from left to right, into a sequence of tokens, as defined by the following grammar.


token      ::= keyword | uN | sN | fN | string | id | ‘(’ | ‘)’ | reserved
keyword    ::= (‘a’ | … |‘z’) idchar* (if occurring as a literal terminal in the grammar)
reserved   ::= idchar+

Tokens are formed from the input character stream according to the longest match rule. That is, the next token always consists of the longest possible sequence of characters that is recognized by the above lexical grammar. Tokens can be separated by white space, but except for strings, they cannot themselves contain whitespace.

Methods

incrementFor

fun <T : Identifier> TextModuleCounts.incrementFor(identifier: T): TextModuleCounts

Receiver

Name Description
TextModuleCounts

Parameters

Name Description
identifier: T

ReturnValue

Name Description
TextModuleCounts

createIdentifier

fun <T : Identifier> createIdentifier(stringValue: String?, intValue: Int?): T?

Creates an Identifier of type T.

Parameters

Name Description
stringValue: String?
intValue: Int?

ReturnValue

Name Description
T?

isKeyword

fun Token.isKeyword(keywordValue: String): Boolean

Determines whether or not the Token is a Keyword matching the provided keywordValue.

Receiver

Name Description
Token

Parameters

Name Description
keywordValue: String

ReturnValue

Name Description
Boolean

asKeywordMatching

fun Token.asKeywordMatching(value: String): Keyword?

Casts the Token into a Keyword and returns it if its Keyword.value is value. Returns null if either condition is unmet.

Receiver

Name Description
Token

Parameters

Name Description
value: String

ReturnValue

Name Description
Keyword?

asKeywordMatching

fun Token.asKeywordMatching(regex: Regex): Pair<Keyword, MatchResult>?

Casts the Token into a Keyword and returns it, along with the match, if its Keyword.value matches regexp.

Returns null if neither condition is met.

Receiver

Name Description
Token

Parameters

Name Description
regex: Regex

ReturnValue

Name Description
Pair<Keyword, MatchResult>?

assertIsKeyword

fun Token.assertIsKeyword(keywordValue: String)

Asserts that the Token is a Keyword matching the provided keywordValue.

Receiver

Name Description
Token

Parameters

Name Description
keywordValue: String

ReturnValue

Name Description
Unit

parseIdentifier

fun <T : Identifier> List<Token>.parseIdentifier(fromIndex: Int, intAllowed: Boolean): ParseResult<T>?

Parses an Identifier of type T from the receiving List of Tokens. If no explicit Identifier is found, an implicit one with Identifier.stringRepr and Identifier.unique both set to null is returned.

Note: Does not support Identifier.TypeDef.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int
intAllowed: Boolean

ReturnValue

Name Description
ParseResult<T>?

parseOrCreateIdentifier

fun <T : Identifier> List<Token>.parseOrCreateIdentifier(fromIndex: Int, counts: TextModuleCounts): Pair, TextModuleCounts><ParseResult<T>, TextModuleCounts>

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int
counts: TextModuleCounts

ReturnValue

Name Description
Pair, TextModuleCounts><ParseResult<T>, TextModuleCounts>

parseLiteral

fun <T : Any> List<Token>.parseLiteral(fromIndex: Int, asClass: KClass<T>): ParseResult<Literal<T>>

Parses a Literal of type T from the receiving List of Tokens at the given position.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int
asClass: KClass<T>

ReturnValue

Name Description
ParseResult<Literal<T>>

isKeyword

fun List<Token>.isKeyword(atIndex: Int, keyword: String): Boolean

Determines whether the Token at the given position is a Keyword matching the provided keyword value.

Receiver

Name Description
List<Token>

Parameters

Name Description
atIndex: Int
keyword: String

ReturnValue

Name Description
Boolean

isOpenParen

fun List<Token>.isOpenParen(atIndex: Int): Boolean

Helper to check if a Token at a given position within the receiving List is a Paren.Open.

Receiver

Name Description
List<Token>

Parameters

Name Description
atIndex: Int

ReturnValue

Name Description
Boolean

isClosedParen

fun List<Token>.isClosedParen(atIndex: Int): Boolean

Helper to check if a Token at a given position within the receiving List is a Paren.Closed.

Receiver

Name Description
List<Token>

Parameters

Name Description
atIndex: Int

ReturnValue

Name Description
Boolean

contextAt

fun List<Token>.contextAt(index: Int): ParseContext?

Helper to get the closest kwasm.format.ParseContext from the given position.

Receiver

Name Description
List<Token>

Parameters

Name Description
index: Int

ReturnValue

Name Description
ParseContext?

getOrThrow

inline fun <T : Token> List<Token>.getOrThrow(index: Int, crossinline message: ()->String): T

Returns the Token as type T from the List at the given index, or throws a ParseException with the result of the message lambda.

Receiver

Name Description
List<Token>

Parameters

Name Description
index: Int
crossinline message: ()->String

ReturnValue

Name Description
T

getOrThrow

fun <T : Token> List<Token>.getOrThrow(index: Int, message: String?): T

Returns the Token as type T from the List at the given index, or throws a ParseException with the provided message (or if null: "Expected T".

Receiver

Name Description
List<Token>

Parameters

Name Description
index: Int
message: String?

ReturnValue

Name Description
T

tokensUntilParenClosure

fun List<Token>.tokensUntilParenClosure(fromIndex: Int, expectedClosures: Int): List<Token>

Finds and returns all tokens between the fromIndex and when the expectedClosures-number of Paren.Closed tokens are found (inclusive).

For example:


(blah (foo (bar) (baz)))

myList.tokensUntilParenClosure(1, 1)

Would return a list of tokens equivalent to blah (foo (bar) (baz)))

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int
expectedClosures: Int

ReturnValue

Name Description
List<Token>