Skip to content

Commit 2128a42

Browse files
docs: updated
1 parent 30e2ff2 commit 2128a42

29 files changed

+373
-66
lines changed

core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/LexerBuilder.kt

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.cybercodernaj.parkour.lexer
22

33
import io.github.cybercodernaj.parkour.lexer.internal.Lexer
4+
import io.github.cybercodernaj.parkour.exceptions.LexicalException
45

56
/**
67
* A helper class to create the [Lexer].
@@ -68,7 +69,7 @@ class LexerBuilder internal constructor() {
6869
var multilineComments: Pair<Regex, Regex>? = Lexer.Defaults.multilineComments
6970

7071
/**
71-
* Supply the regex pattern that defines the rules for identifiers.
72+
* The regex pattern that defines the rules for identifiers.
7273
* Identifiers are parts of the program that are named by the user;
7374
* for instance, the field name or class name.
7475
*
@@ -96,14 +97,59 @@ class LexerBuilder internal constructor() {
9697
private val _operators: MutableList<String> = mutableListOf()
9798
internal val operators: List<String> get() = _operators
9899

99-
internal var integerLiteral: Regex = Lexer.Defaults.integerLiteral
100-
private set
100+
/**
101+
* The regex that detects and extracts integer literals from the string.
102+
* This should usually only consider strict integers.
103+
*
104+
* ### Usage
105+
*
106+
* ```kt
107+
* val myLexer = lexer {
108+
* integerLiteral = Regex("[+-]?[0-9_]+")
109+
* }
110+
* ```
111+
*
112+
* @see Lexer.Defaults.integerLiteral
113+
* @author Nishant Aanjaney Jalan
114+
* @since 0.2.0
115+
*/
116+
var integerLiteral: Regex = Lexer.Defaults.integerLiteral
101117

102-
internal var floatingLiteral: Regex = Lexer.Defaults.floatingLiteral
103-
private set
118+
/**
119+
* The regex that detects and extracts floating point literals from the string.
120+
* This should usually only consider numbers that have a decimal point.
121+
*
122+
* ### Usage
123+
*
124+
* ```kt
125+
* val myLexer = lexer {
126+
* floatingLiteral = Regex("[-+]?[0-9_]*\.[0-9_]+(?:[eE][-+]?[0-9_]+)?")
127+
* }
128+
* ```
129+
*
130+
* @see Lexer.Defaults.floatingLiteral
131+
* @author Nishant Aanjaney Jalan
132+
* @since 0.2.0
133+
*/
134+
var floatingLiteral: Regex = Lexer.Defaults.floatingLiteral
104135

105-
internal var singleLineString: Set<String> = Lexer.Defaults.singleLineString
106-
private set
136+
/**
137+
* The enclosing strings that should denote the start and end of single-line strings.
138+
* The lexer will throw a [LexicalException] when a string literal is not terminated in the same line.
139+
*
140+
* ### Usage
141+
*
142+
* ```kt
143+
* val myLexer = lexer {
144+
* floatingLiteral = Regex("[-+]?[0-9_]*\.[0-9_]+(?:[eE][-+]?[0-9_]+)?")
145+
* }
146+
* ```
147+
*
148+
* @see Lexer.Defaults.singleLineString
149+
* @author Nishant Aanjaney Jalan
150+
* @since 0.2.0
151+
*/
152+
var singleLineString: Set<String> = Lexer.Defaults.singleLineString
107153

108154
/**
109155
* Hard keywords are a characters and symbols that give a particular meaning to a program.
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
# Package io.github.cybercodernaj.parkour.lexer
22

3-
Docs for the lexer
3+
The lexer is responsible to convert the given string into a stream of Tokens.
4+
5+
The lexer takes in multiple settings via the `LexerBuilder` that configures how it behaves.
6+
It performs lexical analysis on a line-by-line basis and returns the next unconsumed token.
7+
A newline character **always** separates a token.
8+
9+
## How to make a Lexer config
10+
11+
You can build your customised Lexer with the `lexer` function.
12+
13+
```kotlin
14+
fun main() {
15+
val myLexer = lexer {
16+
ignorePattern = Regex("""\s+""")
17+
singleLineComments = Regex("//")
18+
/* more configurations */
19+
}
20+
}
21+
```
22+
23+
Find all configuration options and in more detail in `LexerBuilder`.

core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,7 @@ import io.github.cybercodernaj.parkour.lexer.LexerBuilder
66
import io.github.cybercodernaj.parkour.utils.Position
77

88
/**
9-
* The lexer is responsible to convert the given string into a stream of [Token]s.
10-
* The lexer take in multiple settings via the [LexerBuilder] that configures how it behaves.
11-
* It will perform lexical analysis on a line-by-line basis and return the next unconsumed token.
12-
* A newline character **always** separates a token unless it is a multiline comment.
13-
*
14-
* ### Literals
15-
*
16-
* There are only three types of literals the lexer manages.
17-
* 1. Integer literals are normally lexed with a pure stream of numbers with underscores.
18-
* 2. Floating literals are normally lexed with a forced decimal point with optional exponentiation.
19-
* 3. String literals are normally lexed exact strings till it finds the original match.
20-
*
21-
* Additionally, escape sequences are required to input special characters inside string literals.
22-
*
23-
* @constructor Creates a lexer with the provided properties.
24-
* @param ignorePattern characters that satisfy this regex would be skipped.
25-
* @param singleLineComments The regex that defines how a single-line comment starts.
26-
* Once identified, the lexer will skip the remaining line. (Default: null)
27-
* @param multilineComments A pair of regexes, the starting pattern and the ending pattern for a
28-
* multiline comment block. (Default: null)
29-
* @param identifiers A regex string that defines the rules for defining a name.
30-
* @param hardKeywords A set of strings that are considered hard keywords.
31-
* Hard keywords are a characters and symbols that give a particular meaning to a program.
32-
* They may not be used as identifiers. (Default: [])
33-
* @param operators A set of strings that are considered as operators.
34-
* Operators are characters and symbols that may perform arithmetic or logical operations. (Default: [])
35-
* @param separators A set of strings that are considered as separators.
36-
* Separators are characters and symbols that act like delimiters to separate other meaningful elements. (Default: [])
37-
* @param integerLiteral a regex that detects an integer literal.
38-
* @param floatingLiteral a regex that detects a floating point number literal.
39-
* @param singleLineString a set of strings that denote the start and end enclosing strings.
40-
* The lexer will throw a [LexicalException] when a string literal is not terminated in the same line.
41-
* @param escapeSequences a list of regex that matches an escape sequence. On match, it will return a Char based on the string matched.
9+
* This class is internal. You can create an instance of this class by [LexerBuilder]
4210
*
4311
* @author Nishant Aanjaney Jalan
4412
* @since 0.1.0

docs/core/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ <h2 class="">Packages</h2>
9999
</div>
100100
</div>
101101
<div><span class="brief-comment"><a data-name="-545240740%2FPackages%2F537371977" anchor-label="io.github.cybercodernaj.parkour.lexer" id="-545240740%2FPackages%2F537371977" data-filterable-set=":core:dokkaHtmlPartial/main"></a>
102-
<p class="paragraph">Docs for the lexer</p>
102+
<p class="paragraph">The lexer is responsible to convert the given string into a stream of Tokens.</p>
103103
</span></div>
104104
</div>
105105
</div>

docs/core/io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/escape-sequences.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<div class="cover ">
6464
<h1 class="cover"><span>escape</span><wbr><span><span>Sequences</span></span></h1>
6565
</div>
66-
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="escape-sequences.html">escapeSequences</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html">List</a><span class="token operator">&lt;</span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-pair/index.html">Pair</a><span class="token operator">&lt;</span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="token punctuation">, </span><span class="token punctuation">(</span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a><span class="token punctuation">)</span><span class="token operator"> -&gt; </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html">Char</a><span class="token operator">&gt;</span><span class="token operator">&gt;</span><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L81">source</a>)</span></span></div></div></div>
66+
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="escape-sequences.html">escapeSequences</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html">List</a><span class="token operator">&lt;</span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-pair/index.html">Pair</a><span class="token operator">&lt;</span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="token punctuation">, </span><span class="token punctuation">(</span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a><span class="token punctuation">)</span><span class="token operator"> -&gt; </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html">Char</a><span class="token operator">&gt;</span><span class="token operator">&gt;</span><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L49">source</a>)</span></span></div></div></div>
6767
</div>
6868
<div class="footer">
6969
<span class="go-to-top-icon"><a href="#content" id="go-to-top-link"></a></span><span>© 2024 Nishant Aanjaney Jalan</span><span class="pull-right"><span>Generated by </span><a href="https://github.com/Kotlin/dokka"><span>dokka</span><span class="padded-icon"></span></a></span>

docs/core/io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/floating-literal.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<div class="cover ">
6464
<h1 class="cover"><span>floating</span><wbr><span><span>Literal</span></span></h1>
6565
</div>
66-
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="floating-literal.html">floatingLiteral</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L79">source</a>)</span></span></div></div></div>
66+
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="floating-literal.html">floatingLiteral</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L47">source</a>)</span></span></div></div></div>
6767
</div>
6868
<div class="footer">
6969
<span class="go-to-top-icon"><a href="#content" id="go-to-top-link"></a></span><span>© 2024 Nishant Aanjaney Jalan</span><span class="pull-right"><span>Generated by </span><a href="https://github.com/Kotlin/dokka"><span>dokka</span><span class="padded-icon"></span></a></span>

docs/core/io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/identifiers.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<div class="cover ">
6464
<h1 class="cover"><span><span>identifiers</span></span></h1>
6565
</div>
66-
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="identifiers.html">identifiers</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L77">source</a>)</span></span></div></div></div>
66+
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="identifiers.html">identifiers</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L45">source</a>)</span></span></div></div></div>
6767
</div>
6868
<div class="footer">
6969
<span class="go-to-top-icon"><a href="#content" id="go-to-top-link"></a></span><span>© 2024 Nishant Aanjaney Jalan</span><span class="pull-right"><span>Generated by </span><a href="https://github.com/Kotlin/dokka"><span>dokka</span><span class="padded-icon"></span></a></span>

docs/core/io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/ignore-pattern.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<div class="cover ">
6464
<h1 class="cover"><span>ignore</span><wbr><span><span>Pattern</span></span></h1>
6565
</div>
66-
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="ignore-pattern.html">ignorePattern</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L74">source</a>)</span></span></div><p class="paragraph">The lexer will skip over any strings that match this regex. This acts like a token separator.</p><span class="kdoc-tag"><h4 class="">Author</h4><p class="paragraph">Nishant Aanjaney Jalan</p></span><span class="kdoc-tag"><h4 class="">Since</h4><p class="paragraph">0.2.0</p></span></div></div>
66+
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="ignore-pattern.html">ignorePattern</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L42">source</a>)</span></span></div><p class="paragraph">The lexer will skip over any strings that match this regex. This acts like a token separator.</p><span class="kdoc-tag"><h4 class="">Author</h4><p class="paragraph">Nishant Aanjaney Jalan</p></span><span class="kdoc-tag"><h4 class="">Since</h4><p class="paragraph">0.2.0</p></span></div></div>
6767
</div>
6868
<div class="footer">
6969
<span class="go-to-top-icon"><a href="#content" id="go-to-top-link"></a></span><span>© 2024 Nishant Aanjaney Jalan</span><span class="pull-right"><span>Generated by </span><a href="https://github.com/Kotlin/dokka"><span>dokka</span><span class="padded-icon"></span></a></span>

docs/core/io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<div class="breadcrumbs"><a href="../../../index.html">parkour</a><span class="delimiter">/</span><a href="../../index.html">io.github.cybercodernaj.parkour.lexer.internal</a><span class="delimiter">/</span><a href="../index.html">Lexer</a><span class="delimiter">/</span><span class="current">Defaults</span></div>
6363
<div class="cover ">
6464
<h1 class="cover"><span><span>Defaults</span></span></h1>
65-
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">object </span><a href="index.html">Defaults</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L66">source</a>)</span></span></div><p class="paragraph">A list of common patterns and lists of items that most programming languages and data serialization formats.</p><span class="kdoc-tag"><h4 class="">Author</h4><p class="paragraph">Nishant Aanjaney Jalan</p></span><span class="kdoc-tag"><h4 class="">Since</h4><p class="paragraph">0.2.0</p></span></div></div>
65+
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">object </span><a href="index.html">Defaults</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L34">source</a>)</span></span></div><p class="paragraph">A list of common patterns and lists of items that most programming languages and data serialization formats.</p><span class="kdoc-tag"><h4 class="">Author</h4><p class="paragraph">Nishant Aanjaney Jalan</p></span><span class="kdoc-tag"><h4 class="">Since</h4><p class="paragraph">0.2.0</p></span></div></div>
6666
</div>
6767
<div class="tabbedcontent">
6868
<div class="tabs-section" tabs-section="tabs-section"><button class="section-tab" data-active="" data-togglable="CONSTRUCTOR,TYPE,PROPERTY,FUNCTION">Members</button></div>

docs/core/io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/integer-literal.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<div class="cover ">
6464
<h1 class="cover"><span>integer</span><wbr><span><span>Literal</span></span></h1>
6565
</div>
66-
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="integer-literal.html">integerLiteral</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L78">source</a>)</span></span></div></div></div>
66+
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":core:dokkaHtmlPartial/main"><div class="symbol monospace"><span class="token keyword">val </span><a href="integer-literal.html">integerLiteral</a><span class="token operator">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="clearfix"><span class="floating-right">(<a href="https://github.com/cybercoder-naj/parkour/tree/main/core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/internal/Lexer.kt#L46">source</a>)</span></span></div></div></div>
6767
</div>
6868
<div class="footer">
6969
<span class="go-to-top-icon"><a href="#content" id="go-to-top-link"></a></span><span>© 2024 Nishant Aanjaney Jalan</span><span class="pull-right"><span>Generated by </span><a href="https://github.com/Kotlin/dokka"><span>dokka</span><span class="padded-icon"></span></a></span>

0 commit comments

Comments
 (0)