Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍀 Features/minilexer #235

Merged
merged 7 commits into from Jan 28, 2024
Merged

🍀 Features/minilexer #235

merged 7 commits into from Jan 28, 2024

Conversation

hahwul
Copy link
Member

@hahwul hahwul commented Jan 24, 2024

I am adding a lightweight lexer for lexical analysis. It tokenizes the source code. Depending on the mode setting, the tokenize function will decide whether to store the tokenized results. The find function retrieves only the desired type of tokens when in a specific :persistent mode.

a = MiniLexer.new
#<MiniLexer:0x129000c20 @tokens=[], @mode=:normal>

a.mode = :persistent
# :persistent

a.tokenize "foo"
a.tokenize "foo"
a.tokenize "foo"
# <Token:0x129429b00 @type=:identifier, @value="foo", @position=3>
# ....

a.find :identifier
# [
# <Token:0x129429b00 @type=:identifier, @value="foo", @position=3>, 
# <Token:0x11c171fe0 @type=:identifier, @value="foo", @position=3>, 
# <Token:0x119d15ec0 @type=:identifier, @value="foo", @position=3>
# ]

# Token
a.type #=> :identifier
a.value  #=> foo
a.is? :abcd #=> false
a.is? :identifier #=> true

Since token processing varies by language, I think you can inherit MiniLexer and implement the main logic. If you have any good opinions, please share :D

require "../models/minilexer/*"

class SampleLexer < MiniLexer
  def initialize
    super
    # Add custom initial state
  end

  def tokenize_logic
    # Add logic for each state
  end
end

@hahwul hahwul self-assigned this Jan 27, 2024
@hahwul hahwul requested a review from ksg97031 January 27, 2024 14:23
@hahwul hahwul added this to the NextVersion milestone Jan 27, 2024
Copy link
Member

@ksg97031 ksg97031 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!!

@ksg97031 ksg97031 merged commit ad7c2d3 into dev Jan 28, 2024
4 checks passed
@hahwul hahwul deleted the features/minilexer branch January 28, 2024 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants