Skip to content

yassram/SwiLex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

“SwiLex”/

A universal lexer library in Swift.

Introduction

In computer science a Lexer or a tokenizer is a program that converts a sequence of characters (raw string) into a sequence of tokens.

SwiLex is a universal lexer which means that you can use it to build any lexer only by defining your tokens in a Swift enum (in few lines of code).

Combined with SwiParse it allows to build a full Parser. (learn more)

Installation

SwiLex is distributed as a Swift Package using SPM.

To install SwiLex, add the following line to the dependencies list in your Package.swift:

.package(url: "https://github.com/yassram/SwiLex.git", .upToNextMinor(from: "1.1.0")),

SwiLex will support other dependency managers soon.

Usage

Import SwiLex to use it:

import SwiLex

Simple Lexer example

Define a SwiLaxable enum to list your possible tokens with their corresponding regular expressions:

enum WordNumberTokens: String, SwiLexable {
    static var separators: Set<Character> = [" "]
    
    case text = "[A-Za-z]*"
    case number = "[0-9]*"
    
    case eof
    case none
}

Then create an instance of SwiLex and call the lex function:

var lexer = SwiLex<WordNumberTokens>()
let tokenList = try lexer.lex(input: "  H3Ll0   W0r 1d  1234 aBcD ")

// returns [text[H], number[3], text[Ll], number[0], text[W], number[0], 
//          text[r], number[1], text[d], number[1234], text[aBcD]]

This will return a list of tokens with the type of the token and its value (the matched string).

Conditianal tokens Lexer example

SwiLex provides a mechanism for conditionally activating tokens. This tokens are only available for specific Mode.

In the following example we want to make the .quotedString token available only when the .quote mode is active.

Start by defining the possible Modes.

enum Modes {
    case normal
    case quote
}

Then define a SwiLaxable enum to list the possible tokens with their corresponding regular expressions (like for a simple Lexer):

enum QuotedStringTextNumberTokens: String, SwiLexable {
    static var separators: Set<Character> = [" "]
    
    case text = "[A-Za-z]*"
    case number = "[0-9]*"
    case doubleQuote = "\""
    case quotedString = #"[^"]*"#
    
    case eof
    case none
}

Next, define the tokens' availability for the possible modes by implementing the activeForModes property:

extension QuotedStringTextNumberTokens {
var activeForModes: Set<Modes> {
    switch self {
        case .doubleQuote:
            return [.quote, .normal]
        case .quotedString:
            return [.quote]
        default:
            return [.normal]
        }
    }
}
  • .doubleQuote is available for both .normal and .quote modes allowing to switch between them (by defining the end and the begining of a quote).
  • .quotedString is only available for the .quote mode.
  • All the other tokens are only availbale for the .normal mode.

The last step is to tell when to change the curent mode.

For that implement the function changeMode(current mode: Modes?) -> Modes?:

extension QuotedStringTextNumberTokens {
    func changeMode(current mode: Modes?) -> Modes? {
        switch self {
        case .doubleQuote:
            return mode == .normal ? .quote : .normal
        default:
            return mode
        }
    }
}

Finally create an instance of SwiLex and call the lex function with the initial mode:

let input = #"4lb3rt Einstein said "If you can't explain it to a 6 year old, you don't understand it yourself.""#

var lexer = SwiLex<QuotedStringTextNumberTokens>()
let tokenList = try lexer.lex(input: input, initialMode: .normal)

// [number[4], text[lb], number[3], text[rt], text[Einstein], text[said], doubleQuote["], 
//  quotedString[If you can't explain it to a 6 year old, you don't understand it yourself.], 
//  doubleQuote["]]

This will return a list of tokens with the type of the token and its value (the matched string).

Documentation

A documentation with more examples is available here

Features

  • Tokens defined using only a simple SwiLexable enum.
  • Support conditional tokens with custom modes.
  • Support actions on each match by implementing the onLex(raw: Substring) function.
  • Errors with line number and the issue's substring.
  • Add detailed documentation with more examples.
  • Support Cocoapods and Carthage.
  • 🔥 SwiParse, a general-purpose parser generator that can be linked to SwiLex to generate a full parser. (released here) 🔥

Contributing

This is an open source project, so feel free to contribute. How?

  • Open an issue.
  • Send feedback via email.
  • Propose your own fixes, suggestions and open a pull request with the changes.

Author

Yassir Ramdani

License

MIT License

Copyright (c) 2020 yassir RAMDANI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.