Skip to content

Commit 0591674

Browse files
authored
Merge pull request #15 from Dhritikrishna123/dev
feat: change the lexer to make it more modular
2 parents f51979b + 3391cda commit 0591674

File tree

12 files changed

+516
-883
lines changed

12 files changed

+516
-883
lines changed

main.py

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
"""Module providing Lexer Function"""
2-
from src.lexer import lexer
3-
4-
source_code = '''
5-
# Variable declarations and assignments
6-
quack("Hello")
7-
let my_count = ((( 7 + my_idea ) * 7437 + 949) == 84)
8-
if (x = 10):
9-
y = x + 20 * 3
10-
if (a == b):
11-
quack(90)
12-
'''
13-
14-
def run_lexer(source_code):
15-
'''Call the lexer to tokenize the source code'''
16-
tokens = lexer(source_code)
17-
18-
# Print header
19-
print(f"{'Token Type':<25} {'Value':<30} {'Position'}")
20-
print("="*80) # Separator line
21-
22-
# Print each token in a well-formatted way
23-
for token in tokens:
24-
token_type = token['type']
25-
value = token['value']
26-
position = token['position']
27-
28-
# Print token info with proper formatting
29-
print(f"{token_type:<25} {str(value):<30} {position}")
30-
31-
if __name__ == "__main__":
32-
run_lexer(source_code)
1+
from src.lexer import Lexer
2+
3+
# Sample source code input
4+
source_code = """
5+
def match(self, text):
6+
if self.source[self.position.index:self.position.index + len(text)] -> == text:
7+
for . in range(len(text)):
8+
self.advance()
9+
return True
10+
return False
11+
12+
"""
13+
14+
# Initialize lexer
15+
lexer = Lexer()
16+
tokens = lexer.tokenize(source_code)
17+
18+
# Print detailed token information
19+
print("\n📝 Token Details:")
20+
for token in tokens:
21+
print(f"🔹 {token}")
22+
23+
# Print a nicely formatted table
24+
print("\n📌 Token Table:")
25+
26+
# Define column widths
27+
col_widths = [30, 12, 14, 12, 6, 8]
28+
line_sep = "+" + "+".join(["-" * w for w in col_widths]) + "+"
29+
30+
# Print table header
31+
print(line_sep)
32+
print(f"| {'Type':<20} | {'Value':<12} | {'Start Index':<14} | {'End Index':<12} | {'Line':<6} | {'Column':<8} |")
33+
print(line_sep)
34+
35+
# Print table rows
36+
for token in tokens:
37+
print(f"| {str(token.token_type):<20} | {str(token.value):<12} | {str(token.start_pos.index):<14} | {str(token.end_pos.index):<12} | {str(token.start_pos.line):<6} | {str(token.start_pos.column):<8} |")
38+
39+
# Print table footer
40+
print(line_sep)

0 commit comments

Comments
 (0)