Slide 1

Slide 1 text

Vincent Salamanca-Gagnon Android Developer, Transit Parsing Inline Strings Building a Small Cross-Platform Library

Slide 2

Slide 2 text

Outline • Demonstrate library use case • Design decisions • Implementation of a string parser • Learn something new

Slide 3

Slide 3 text

Strings

Slide 4

Slide 4 text

DevFest Montreal 2024

Slide 5

Slide 5 text

DevFest Montreal 2024

Slide 6

Slide 6 text

DevFest Montreal 2024

Slide 7

Slide 7 text

DevFest Montreal 2024

Slide 8

Slide 8 text

DevFest Montreal 2024

Slide 9

Slide 9 text

DevFest Montreal 2024 buildAnnotatedString { }

Slide 10

Slide 10 text

DevFest Montreal 2024 buildAnnotatedString { append("DevFest Montreal ") }

Slide 11

Slide 11 text

DevFest Montreal 2024 buildAnnotatedString { append("DevFest Montreal ") withStyle( style = SpanStyle(fontWeight = FontWeight.Bold) ) { } }

Slide 12

Slide 12 text

DevFest Montreal 2024 buildAnnotatedString { append("DevFest Montreal ") withStyle( style = SpanStyle(fontWeight = FontWeight.Bold) ) { append("2024") } }

Slide 13

Slide 13 text

Issues • Ressources? • Accessibility? • User preference? • Dynamic?

Slide 14

Slide 14 text

Library

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Design Guidelines • Avoid code duplication between platforms • Make it easy to extend and flexible • Minimize API surface • Performance

Slide 17

Slide 17 text

Design Guidelines • Avoid code duplication between platforms • Make it easy to extend and flexible • Minimize API surface • Performance

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Kotlin Multiplatform (KMP) • Kotlin ❤ • Incremental Adoption • Flexible API boundary

Slide 20

Slide 20 text

Flexible API Boundary

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

KMP

Slide 23

Slide 23 text

KMP

Slide 24

Slide 24 text

KMP

Slide 25

Slide 25 text

KMP

Slide 26

Slide 26 text

Design Considerations • Avoid code duplication between platforms • Make it easy to extend and flexible • Minimize API surface • Performance

Slide 27

Slide 27 text

Design Considerations • Avoid code duplication between platforms ✅ • Make it easy to extend and flexible • Minimize API surface • Performance

Slide 28

Slide 28 text

Design Considerations • Avoid code duplication between platforms ✅ • Make it easy to extend and flexible • Minimize API surface • Performance

Slide 29

Slide 29 text

Language?

Slide 30

Slide 30 text

JSON? • Flexible ✅ • Typed ❌ • Ordering ❌ • Performance ? {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } } }

Slide 31

Slide 31 text

HTML • Markup language ✅

My First Heading h1>

My first paragraph. p> < / body> < / html>

Slide 32

Slide 32 text

HTML • Markup language

Slide 33

Slide 33 text

Domain Specific Language (DSL) Stop: Sherbrooke 8 : 10PM Stop: bold> Sherbrooke • Minimal ✅ • Flexible/Extensible ✅ • Typed ✅ • Performance*

Slide 34

Slide 34 text

Keywords • • • • • • …

Slide 35

Slide 35 text

Stop: bold> Sherbrooke

Slide 36

Slide 36 text

Stop: bold> Sherbrooke

Slide 37

Slide 37 text

Stop: Sherbrooke 8 : 10PM

Slide 38

Slide 38 text

Stop: Sherbrooke 8 : 10PM Stop: Sherbrooke 20 : 10 Stop: Sherbrooke 8 : 10PM

Slide 39

Slide 39 text

Compiler?

Slide 40

Slide 40 text

String Parser

Slide 41

Slide 41 text

Implementation

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Our Library

Slide 44

Slide 44 text

Lexer

Slide 45

Slide 45 text

Stop: < / bold> Sherbrooke Stop: < / bold> Sherbrooke

Slide 46

Slide 46 text

Tokens content NAME> • TAGBEGIN: • TAGCLOSE: NAME> • PARAMETER: between | • CONTENT: Text between tags

Slide 47

Slide 47 text

Token Class sealed class Token { data class TagBegin(val name: String, val position: Int) : Token() data class TagEnd(val position: Int) : Token() data class TagClose(val name: String, val position: Int) : Token() data class Parameter(val value: String, val position: Int) : Token() data class Content(val text: String, val position: Int) : Token() }

Slide 48

Slide 48 text

Lexer Class class Lexer(private val input: String) { private var position: Int = 0 private val length = input.length fun lex(): List { val tokens = mutableListOf() return tokens } }

Slide 49

Slide 49 text

Lexer Class class Lexer(private val input: String) { private var position: Int = 0 private val length = input.length fun lex(): List { val tokens = mutableListOf() while (position < length) { val currentChar = input[position] if (currentChar == '<') { tokens.addAll(readTag()) } else { tokens.add(readContent()) } } return tokens } }

Slide 50

Slide 50 text

private fun readContent(): Token.Content { val startPos = position val content = StringBuilder() return Token.Content(content.toString(), startPos) } readContent()

Slide 51

Slide 51 text

private fun readContent(): Token.Content { val startPos = position val content = StringBuilder() while (position < length && input[position] != '<') { content.append(input[position]) position++ } return Token.Content(content.toString(), startPos) } readContent()

Slide 52

Slide 52 text

private fun readTag(): List { val tokens = mutableListOf() val startPos = position position++ // Skip '<' return tokens } readTag()

Slide 53

Slide 53 text

private fun readTag(): List { val tokens = mutableListOf() val startPos = position position++ // Skip '<' if (input[position] == '/') { position++ // Skip '/' val name = readName() tokens.add(Token.TagClose(name, startPos)) expectChar('>') position++ // Skip '>' } else { } return tokens } readTag()

Slide 54

Slide 54 text

val name = readName() tokens.add(Token.TagBegin(name, startPos)) while (position < length && input[position] != '>') { } tokens.add(Token.TagEnd(position)) position++ // Skip '>' readTag()

Slide 55

Slide 55 text

readTag() val name = readName() tokens.add(Token.TagBegin(name, startPos)) while (position < length && input[position] != '>') { if (input[position] == '|') { position++ // Skip '|' val param = readParameter() tokens.add(Token.Parameter(param, position)) } else if (input[position].isWhitespace()) { position++ // Skip whitespace } else { throw ParsingException("Unexpected character '${input[position]}' in tag", position) } } tokens.add(Token.TagEnd(position)) position++ // Skip '>'

Slide 56

Slide 56 text

readName() private fun readName(): String { val nameBuilder = StringBuilder() while (position < length && input[position].isLetterOrDigit()) { nameBuilder.append(input[position]) position++ } if (nameBuilder.isEmpty()) { throw ParsingException("Tag name cannot be empty", position) } return nameBuilder.toString() }

Slide 57

Slide 57 text

class ParsingException(message: String, val position: Int) : Exception("$message at position $position”) // ParsingException: Expected '>' at the end of tag at position 26 ParsingException

Slide 58

Slide 58 text

Result [ Content(text="Hello, ", position=0), TagBegin(name="bold", position=7), Parameter(value="param1", position=13), Parameter(value="param2", position=20), TagEnd(position=26), Content(text="world", position=27), TagClose(name="bold", position=32), Content(text="!", position=38) ] Hello word < / bold>!

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Parser • Add structure • Validate syntax • Output Abstract Syntax Tree (AST)

Slide 61

Slide 61 text

Grammars • Terminals → Text Image Date • Non-Terminals → Bold Light Color • Start Symbol

Slide 62

Slide 62 text

Grammars Production rules • Non-Terminal -> Terminal | Non-Terminal

Slide 63

Slide 63 text

AST Hello word 8 : 10am color> bold>

Slide 64

Slide 64 text

StringElement interface StringElement data class TextElement(val content: String) : StringElement data class BoldElement(val content: List) : StringElement

Slide 65

Slide 65 text

API

Slide 66

Slide 66 text

Design Considerations • Avoid code duplication between platforms ✅ • Make it easy to extend and flexible ✅ • Minimize API surface • Performance

Slide 67

Slide 67 text

Walking the tree [ StyledElement(content = "Hello, ", styles = {}), StyledElement(content = "world", styles = {Style.BOLD}), StyledElement(content = “8:10AM”, styles = {Style.BOLD, Style.COLOR}), … ]

Slide 68

Slide 68 text

API // commonMain fun parseStyledText(input: String): List { val tokens = Lexer(input).lex() // Lexing val ast = Parser(tokens).parse() // Parsing val styledElements = ast. fl atMap { element -> fl attenAST(element) } // Flattening return styledElements }

Slide 69

Slide 69 text

API // commonMain fun parseStyledText(input: String): List { val tokens = Lexer(input).lex() // Lexing val ast = Parser(tokens).parse() // Parsing val styledElements = ast. fl atMap { element -> fl attenAST(element) } // Flattening return styledElements } // Android @Composable fun InlineStringComposable(input: String) { val styledElements = parseStyledText(input) val annotatedString = buildAnnotatedString { styledElements.forEach { element -> // render styled element } } }

Slide 70

Slide 70 text

Design Considerations • Avoid code duplication between platforms ✅ • Make it easy to extend and flexible ✅ • Minimize API surface ✅ • Performance

Slide 71

Slide 71 text

• Memory allocation • No code • Combine Lexer/Parser • Caching Performance

Slide 72

Slide 72 text

Going further • 100% KMP • JIT • Test Coverage • Versioning

Slide 73

Slide 73 text

References • Crafting Interpreters by Robert Nystrom craftinginterpreters.com

Slide 74

Slide 74 text

Thank you

Slide 75

Slide 75 text

Questions Vincent Salamanca-Gagnon @vinsg_ [email protected] Crafting Interpreters by Robert Nystrom craftinginterpreters.com