Skip to content

Commit fbd33d5

Browse files
feat: added identifiers to the dsl
1 parent 39e4c1c commit fbd33d5

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ class LexerBuilder internal constructor() {
2020
internal var multilineComments: Pair<Regex, Regex>? = Lexer.Defaults.multilineComments
2121
private set
2222

23+
internal var identifiers: Regex = Lexer.Defaults.identifiers
24+
private set
25+
2326
/**
2427
* The lexer will skip over any strings that match this regex.
2528
* This acts like a token separator.
2629
*
30+
* ### Usage
31+
*
2732
* ```kt
2833
* val myLexer = lexer {
2934
* ignorePatterns(Regex("""\s+"""))
@@ -44,6 +49,8 @@ class LexerBuilder internal constructor() {
4449
* When the lexer identifies a [singleLineComments] pattern, it will skip to the next line
4550
* and return the next token.
4651
*
52+
* ### Usage
53+
*
4754
* ```kt
4855
* val myLexer = lexer {
4956
* singleLineComments(Regex("//"))
@@ -65,14 +72,16 @@ class LexerBuilder internal constructor() {
6572
* When the lexer identifies the starting pattern, it will continue to skip to the next possible token
6673
* until it meets the ending pattern.
6774
*
75+
* ### Usage
76+
*
6877
* ```kt
6978
* val myLexer = lexer {
7079
* // You don't need the square brackets but KDoc doesn't like it...
7180
* multilineComments(Regex("[/][*]") to Regex("[*][/]"))
7281
* }
7382
* ```
7483
*
75-
* @param multilineComments regex of the pattern the lexer will skip over to the next line.
84+
* @param multilineComments pair of regexes that define what contents are under comments.
7685
*
7786
* @see Lexer.Defaults.multilineComments
7887
* @author Nishant Aanjaney Jalan
@@ -81,6 +90,30 @@ class LexerBuilder internal constructor() {
8190
fun multilineComments(multilineComments: Pair<Regex, Regex>) {
8291
this.multilineComments = multilineComments
8392
}
93+
94+
/**
95+
* Supply the regex pattern that defines the rules for identifiers.
96+
* Identifiers are parts of the program that are named by the user;
97+
* for instance, the field name or class name.
98+
*
99+
* ### Usage
100+
*
101+
* ```kt
102+
* val myLexer = lexer {
103+
* // If all identifiers must be at least 2 characters and start with a lowercase english alphabet.
104+
* identifiers(Regex("[a-z][a-zA-Z0-9]+"))
105+
* }
106+
* ```
107+
*
108+
* @param identifiers regex defining the rules of the naming identifiers
109+
*
110+
* @see Lexer.Defaults.multilineComments
111+
* @author Nishant Aanjaney Jalan
112+
* @since 0.2.0
113+
*/
114+
fun identifiers(identifiers: Regex) {
115+
this.identifiers = identifiers
116+
}
84117
}
85118

86119
/**
@@ -108,6 +141,7 @@ fun lexer(init: LexerBuilder.() -> Unit): Lexer {
108141
return Lexer(
109142
ignorePattern = builder.ignorePattern,
110143
singleLineComments = builder.singleLineComments,
111-
multilineComments = builder.multilineComments
144+
multilineComments = builder.multilineComments,
145+
identifiers = builder.identifiers
112146
)
113147
}

core/src/test/kotlin/io/github/cybercodernaj/parkour/lexer/LexerBuilderTest.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test
99
class LexerBuilderTest {
1010
@Test
1111
fun `initialise a default lexer`() {
12-
val myLexer = lexer {}
12+
lexer {}
1313
}
1414

1515
@Test
@@ -59,5 +59,21 @@ class LexerBuilderTest {
5959
)
6060
)
6161
}
62+
63+
@Test
64+
fun `sets identifiers rule`() {
65+
val myLexer = lexer {
66+
identifiers(Regex("[a-z][a-zA-Z0-9]+"))
67+
}
68+
69+
myLexer.source = StringSource("hi value")
70+
assertTokens(
71+
myLexer,
72+
listOf(
73+
Token.Identifier("hi", Position(0, 0), Position(0, 1)),
74+
Token.Identifier("value", Position(0, 3), Position(0, 7))
75+
)
76+
)
77+
}
6278
}
6379

docs/core/io.github.cybercodernaj.parkour.lexer/-lexer-builder/multiline-comments.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>multiline</span><wbr><span><span>Comments</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">fun </span><a href="multiline-comments.html"><span class="token function">multilineComments</span></a><span class="token punctuation">(</span><span class="parameters "><span class="parameter ">multilineComments<span class="token operator">: </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><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="token operator">&gt;</span></span></span><span class="token punctuation">)</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/LexerBuilder.kt#L81">source</a>)</span></span></div><p class="paragraph">There are two parts to <a href="multiline-comments.html">multilineComments</a>: the starting and the ending pattern. When the lexer identifies the starting pattern, it will continue to skip to the next possible token until it meets the ending pattern.</p><div class="sample-container"><pre><code class="block lang-kt" theme="idea">val myLexer = lexer {<br> // You don't need the square brackets but KDoc doesn't like it...<br> multilineComments(Regex("[/][*]") to Regex("[*][/]"))<br>}</code></pre><span class="top-right-position"><span class="copy-icon"></span><div class="copy-popup-wrapper popup-to-left"><span class="copy-popup-icon"></span><span>Content copied to clipboard</span></div></span></div><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><h4 class="">Parameters</h4><div class="table"><div class="table-row" data-filterable-current=":core:dokkaHtmlPartial/main" data-filterable-set=":core:dokkaHtmlPartial/main"><div class="main-subrow keyValue "><div class=""><span class="inline-flex"><div><u><span>single</span><wbr><span>Line</span><wbr><span><span>Comments</span></span></u></div></span></div><div><div class="title"><p class="paragraph">regex of the pattern the lexer will skip over to the next line.</p></div></div></div></div></div><h4 class="">See also</h4><div class="table"><div class="table-row" data-filterable-current=":core:dokkaHtmlPartial/main" data-filterable-set=":core:dokkaHtmlPartial/main"><div class="main-subrow keyValue "><div class=""><span class="inline-flex"><div><a href="../../io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/single-line-comments.html"><span>Lexer.</span><wbr><span>Defaults.</span><wbr><span>single</span><wbr><span>Line</span><wbr><span><span>Comments</span></span></a></div></span></div><div></div></div></div></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">fun </span><a href="multiline-comments.html"><span class="token function">multilineComments</span></a><span class="token punctuation">(</span><span class="parameters "><span class="parameter ">multilineComments<span class="token operator">: </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><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html">Regex</a><span class="token operator">&gt;</span></span></span><span class="token punctuation">)</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/LexerBuilder.kt#L81">source</a>)</span></span></div><p class="paragraph">There are two parts to <a href="multiline-comments.html">multilineComments</a>: the starting and the ending pattern. When the lexer identifies the starting pattern, it will continue to skip to the next possible token until it meets the ending pattern.</p><div class="sample-container"><pre><code class="block lang-kt" theme="idea">val myLexer = lexer {<br> // You don't need the square brackets but KDoc doesn't like it...<br> multilineComments(Regex("[/][*]") to Regex("[*][/]"))<br>}</code></pre><span class="top-right-position"><span class="copy-icon"></span><div class="copy-popup-wrapper popup-to-left"><span class="copy-popup-icon"></span><span>Content copied to clipboard</span></div></span></div><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><h4 class="">Parameters</h4><div class="table"><div class="table-row" data-filterable-current=":core:dokkaHtmlPartial/main" data-filterable-set=":core:dokkaHtmlPartial/main"><div class="main-subrow keyValue "><div class=""><span class="inline-flex"><div><u><span>multiline</span><wbr><span><span>Comments</span></span></u></div></span></div><div><div class="title"><p class="paragraph">regex of the pattern the lexer will skip over to the next line.</p></div></div></div></div></div><h4 class="">See also</h4><div class="table"><div class="table-row" data-filterable-current=":core:dokkaHtmlPartial/main" data-filterable-set=":core:dokkaHtmlPartial/main"><div class="main-subrow keyValue "><div class=""><span class="inline-flex"><div><a href="../../io.github.cybercodernaj.parkour.lexer.internal/-lexer/-defaults/multiline-comments.html"><span>Lexer.</span><wbr><span>Defaults.</span><wbr><span>multiline</span><wbr><span><span>Comments</span></span></a></div></span></div><div></div></div></div></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)