halotukozak.alpaca

Members list

Packages

Type members

Experimental classlikes

final class IgnoredToken[+Ctx <: LexerCtx] extends Token[ValidName, Ctx, Nothing]

Represents a specific type of token definition that denotes an ignored token during the lexing process.

Represents a specific type of token definition that denotes an ignored token during the lexing process.

Ignored tokens typically refer to tokens that are matched and processed by the lexer but are excluded from the final parsed token stream. Examples of ignored tokens include whitespace, comments, or any other tokens that are syntactically meaningful but do not contribute to the structured representation of the input source.

This opaque type is parameterized by a context type Ctx, which must be a subtype of LexerCtx. The LexerCtx trait serves as a base for maintaining global lexing state, such as the current position, the last matched token, and the remaining input.

The ValidName and Nothing type parameters are placeholder constraints inherited from Token, but IgnoredToken does not provide its own additional constraints or behavior beyond being excluded from normal processing.

The use of an opaque type ensures safe and restricted use within the scope of the lexer, as this type cannot be directly manipulated outside the context of its definition.

Attributes

Experimental
true
Source
lexer.scala
Supertypes
class Token[ValidName, Ctx, Nothing]
class Object
trait Matchable
class Any
trait LexerCtx extends Product, Selectable

Trait for the global context used during tokenization.

Trait for the global context used during tokenization.

The global context maintains state during lexing, including the current position in the input, the last matched token, and the remaining text to process. Users can extend this trait to add custom state tracking.

Attributes

Companion
object
Experimental
true
Source
lexer.scala
Supertypes
trait Selectable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Known subtypes
class Default
class Empty
object LexerCtx

Attributes

Companion
trait
Experimental
true
Source
lexer.scala
Supertypes
class Object
trait Matchable
class Any
Self type
LexerCtx.type
trait ParserCtx

Base trait for parser global context.

Base trait for parser global context.

Unlike the lexer, the parser's global context is typically empty by default, but can be extended to track custom state during parsing such as symbol tables, type information, or other semantic data.

Attributes

Companion
object
Experimental
true
Source
parser.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Empty
object ParserCtx

Attributes

Companion
trait
Experimental
true
Source
parser.scala
Supertypes
class Object
trait Matchable
class Any
Self type
ParserCtx.type
object Production

Attributes

Experimental
true
Source
parser.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Production.type
sealed trait ResolutionCtx[P <: Parser[_]]

Attributes

Companion
object
Experimental
true
Source
parser.scala
Supertypes
class Object
trait Matchable
class Any
object ResolutionCtx

Attributes

Companion
trait
Experimental
true
Source
parser.scala
Supertypes
class Object
trait Matchable
class Any
Self type
trait Rule[R]

Represents a grammar rule in the parser.

Represents a grammar rule in the parser.

A rule defines how a non-terminal symbol can be parsed by specifying one or more productions. Each production maps a pattern of symbols to a result value.

Rules are created using the rule function and can be used in pattern matching within parser productions.

Type parameters

R

the type of value produced when this rule is matched

Attributes

Experimental
true
Source
parser.scala
Supertypes
class Object
trait Matchable
class Any
class Token[+Name <: ValidName, +Ctx <: LexerCtx, +Value]

Defines an opaque type Token that represents a token used in a lexer.

Defines an opaque type Token that represents a token used in a lexer.

This type has three type parameters:

  • Name: The type of the token's name, restricted to a subtype of ValidName.
  • Ctx: The type of the lexer context, restricted to a subtype of LexerCtx.
  • Value: The type of the token's value.

The exact implementation details of the underlying type are abstracted away by using Any. Opaque types provide type safety without exposing the underlying representation.

Attributes

Companion
object
Experimental
true
Source
lexer.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class IgnoredToken[Ctx]
object Token

Factory methods for creating token definitions in the lexer DSL.

Factory methods for creating token definitions in the lexer DSL.

Attributes

Companion
class
Experimental
true
Source
lexer.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Token.type

Types

Type representing conflict resolution rules for the parser.

Type representing conflict resolution rules for the parser.

Conflict resolutions are used to resolve shift/reduce and reduce/reduce conflicts in the parsing table by specifying precedence relationships between productions and tokens.

Attributes

Source
parser.scala
type LexerDefinition[Ctx <: LexerCtx] = PartialFunction[String, Token[ValidName, Ctx, Any]]

Type alias for lexer rule definitions.

Type alias for lexer rule definitions.

A lexer definition is a partial function that maps string patterns (as regex literals) to token definitions.

Type parameters

Ctx

the global context type

Attributes

Source
lexer.scala
type Parser[Ctx <: ParserCtx] = Parser[Ctx]

Attributes

Source
parser.scala
type ProductionDefinition[R] = PartialFunction[Tuple | Lexeme[_, _], R]

Defines a single production in a grammar rule.

Defines a single production in a grammar rule.

A production definition is a partial function that matches a specific pattern of symbols (as a tuple of terminals and non-terminals, or a single lexeme) and produces a result value of type R. Productions are the building blocks of grammar rules, specifying how input sequences are recognized and transformed.

Production definitions are typically passed to the rule function to define the possible ways a non-terminal can be parsed.

See the documentation for rule for more details.

Type parameters

R

the result type produced by this production

Attributes

Source
parser.scala
opaque type Resolutions[P <: Parser[_]]

Attributes

Source
parser.scala
type SepValue[S] = S match { case Token[n, _$2, v] => Lexeme[n, v] case Rule[t] => t }

The runtime value type of a separator symbol used by .SeparatedBy.

The runtime value type of a separator symbol used by .SeparatedBy.

The parser places Lexeme values on the stack for terminals, so when the separator is a token type Token[n, ?, v], its runtime value is Lexeme[n, v]. For a rule separator Rule[t] (typically passed as a singleton type like Sep.type), the runtime value is t — whatever that rule produces.

Type parameters

S

the separator symbol type (a token type or a rule's .type)

Attributes

Source
parser.scala

Value members

Experimental methods

transparent inline def ctx[C <: LexerCtx](using c: C): C

Propagates the lexer context through the DSL so that token constructors and rule bodies can access it implicitly.

Propagates the lexer context through the DSL so that token constructors and rule bodies can access it implicitly.

The returned type is the concrete context type C refined with a getter and a setter for every case field that doesn't already have a real setter, e.g. for case class Ctx(count: Int): C { def count: Int; def count_=(v: Int): Unit }. This is what lets ctx.count += 1 type-check even when count is an immutable val — the real getter always wins over the structural one, but the structural setter is used since there is no real one. The lexer macro then rewrites every such structural assignment back into a copy (see RewriteCtxMutations) before the rule is compiled, so the structural setter is never actually invoked at runtime for a case class context: this type exists purely to make the mutation-looking syntax type-check. Contexts that still declare var fields are unaffected: the real var setter shadows the structural one and the assignment mutates in place, exactly as before, and in fact never gains a refinement member in the first place.

C is inferred as a fresh, unbound type parameter from whatever context function currently binds c — deliberately not c.type: refining the singleton type of the specific enclosing lambda parameter, rather than the nominal class C, is what a lexer rule's own macro (which tears the rule apart and rebuilds its pieces as fresh lambdas — see Lexer.scala) empirically stumbles on downstream, even though the two only differ in which stable path they're attached to.

Attributes

Experimental
true
Source
lexer.scala
transparent inline def lexer[Ctx <: LexerCtx](using withDefault[Ctx, Default])(inline rules: Ctx ?=> LexerDefinition[Ctx])(using m: ProductOf[Ctx], errorHandling: ErrorHandling[Ctx], empty: Empty[Ctx]): Tokenization[Ctx] { type LexemeFields = NamedTuple[m.MirroredElemLabels, m.MirroredElemTypes]; }

Creates a lexer from a DSL-based definition.

Creates a lexer from a DSL-based definition.

This is the main entry point for defining a lexer. It uses a macro to compile the lexer definition into efficient tokenization code.

Example:

val myLexer = lexer {
 case "\\d+" => Token["number"]
 case "[a-zA-Z]+" => Token["identifier"]
 case "\\s+" => Token.Ignored
}

Type parameters

Ctx

the global context type, defaults to LexerCtx.Default

Value parameters

empty

implicit Empty instance to create the initial context

errorHandling

implicit ErrorHandling for custom error recovery

rules

the lexer rules as a partial function

Attributes

Returns

a Tokenization instance that can tokenize input strings

Experimental
true
Source
lexer.scala
transparent inline def production[P <: Parser[_]](using ResolutionCtx[P]): ProductionSelector

Attributes

Experimental
true
Source
parser.scala
def resolutions[P <: Parser[_]](elements: ResolutionCtx[P] ?=> ConflictResolution*): Resolutions[P]

Attributes

Experimental
true
Source
parser.scala
inline def rule[R](productions: ProductionDefinition[R]*): Rule[R]

Creates a grammar rule from one or more productions.

Creates a grammar rule from one or more productions.

This is the main way to define grammar rules in the parser DSL. Each production is a partial function that matches a pattern of symbols (terminals and non-terminals) and produces a result value.

This is compile-time only and should only be used inside parser class definitions.

Example:

val expr: Rule[Int] = rule(
 { case (number(a), Lexer.+(_), number(b)) => a.toInt + b.toInt },
 { case (number(n)) => n.toInt }
)

Type parameters

R

the result type produced by this rule

Value parameters

productions

one or more productions that define this rule

Attributes

Returns

a Rule instance

Experimental
true
Source
parser.scala

Extensions

Experimental extensions

extension (first: Production | Token[_, _, _])
infix inline def after(second: Production | Token[_, _, _]*): ConflictResolution

Specifies that this production/token should have higher precedence than others.

Specifies that this production/token should have higher precedence than others.

This is compile-time only and should only be used inside parser rule definitions.

Example:

Production(expr, "*", expr) after Production(expr, "+", expr)

Value parameters

second

the productions/tokens that should have lower precedence

Attributes

Returns

a conflict resolution rule

Experimental
true
Source
parser.scala
infix inline def before(second: Production | Token[_, _, _]*): ConflictResolution

Specifies that this production/token should have lower precedence than others.

Specifies that this production/token should have lower precedence than others.

This is compile-time only and should only be used inside parser rule definitions.

Example:

Production(expr, "+", expr) before Production(expr, "*", expr)

Value parameters

second

the productions/tokens that should have higher precedence

Attributes

Returns

a conflict resolution rule

Experimental
true
Source
parser.scala
extension (name: String)
inline def apply[R](production: ProductionDefinition[R]): production.type

Defines a named production for use in grammar rules and conflict resolution.

Defines a named production for use in grammar rules and conflict resolution.

This extension method allows you to assign a name to a specific production within a rule. Named productions can be referenced in conflict resolution rules using the Production selector, enabling fine-grained control over precedence and associativity.

Usage:

val add: Rule[Int] = rule(
 "sum" { case (number(a), Lexer.+(_), number(b)) => a.toInt + b.toInt },
 { case (number(n)) => n.toInt }
)

// In conflict resolution:
given Resolutions[MyParser.type] = resolutions(
 production.sum.after(Lexer.+),
)

Type parameters

R

the result type produced by this production

Value parameters

production

the production to name

Attributes

Returns

the original production, annotated with the given name

Experimental
true
Source
parser.scala
extension [Ctx <: ParserCtx](parser: Parser[Ctx])
inline def parse(lexems: List[Lexeme[_, _]]): (ctx: Ctx, result: parser.root.type match { case Rule[t] => t } | Null)

Parses a list of lexemes using the defined grammar.

Parses a list of lexemes using the defined grammar.

This is a convenience method that infers the result type from the root rule.

Value parameters

lexems

the list of lexemes to parse

Attributes

Returns

a tuple of (context, result), where result may be null on parse failure

Experimental
true
Source
parser.scala

Exports

Defined exports

final val Column: Column
Exported from Column$package

Attributes

Source
lexer.scala
Exported from lexer

Attributes

Source
lexer.scala
final val LazyReader: LazyReader
Exported from lexer

Attributes

Source
lexer.scala
final val Line: Line
Exported from Line$package

Attributes

Source
lexer.scala
final val Tracking: Tracking
Exported from lexer

Attributes

Source
lexer.scala