Skip to content

Commit

Permalink
added python type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-schwenk committed Feb 16, 2024
1 parent 45872b0 commit c5da0c4
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions sell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import json, sys, os, re
from typing import Self


class Lexer:
Expand Down Expand Up @@ -214,7 +215,7 @@ def parse(self):
else:
raise Exception("unimplemented")

def parse_span(self, lex: Lexer):
def parse_span(self, lex: Lexer) -> Self:
# grammar: span = { item };
# item = bold | math | text | input;
# bold = "*" { item } "*";
Expand All @@ -226,7 +227,7 @@ def parse_span(self, lex: Lexer):
span.children.append(self.parse_item(lex))
return span

def parse_item(self, lex: Lexer, math_mode=False):
def parse_item(self, lex: Lexer, math_mode=False) -> Self:
if not math_mode and lex.token == "*":
return self.parse_bold_italic(lex)
elif lex.token == "$":
Expand All @@ -243,7 +244,7 @@ def parse_item(self, lex: Lexer, math_mode=False):
lex.next()
return n

def parse_bold_italic(self, lex: Lexer):
def parse_bold_italic(self, lex: Lexer) -> Self:
node = TextNode("italic")
if lex.token == "*":
lex.next()
Expand All @@ -258,7 +259,7 @@ def parse_bold_italic(self, lex: Lexer):
lex.next()
return node

def parse_math(self, lex: Lexer):
def parse_math(self, lex: Lexer) -> Self:
math = TextNode("math")
if lex.token == "$":
lex.next()
Expand All @@ -273,7 +274,7 @@ def parse_math(self, lex: Lexer):
lex.next()
return math

def parse_input(self, lex: Lexer):
def parse_input(self, lex: Lexer) -> Self:
input = TextNode("input")
if lex.token == "%":
lex.next()
Expand All @@ -284,7 +285,7 @@ def parse_input(self, lex: Lexer):
lex.next()
return input

def optimize(self):
def optimize(self) -> Self:
children_opt = []
for c in self.children:
opt = c.optimize()
Expand Down Expand Up @@ -347,7 +348,7 @@ def build(self):
self.post_process_text(self.text)
self.text.optimize()

def post_process_text(self, node, math=False):
def post_process_text(self, node: TextNode, math=False):
for c in node.children:
self.post_process_text(
c, math or node.type == "math" or node.type == "display-math"
Expand Down Expand Up @@ -380,7 +381,7 @@ def post_process_text(self, node, math=False):
node.type = "code"
node.data = node.data[1:-1]

def format_float(self, v) -> str:
def format_float(self, v: float) -> str:
s = str(v)
if s.endswith(".0"):
return s[:-2]
Expand Down Expand Up @@ -477,7 +478,7 @@ def to_dict(self) -> dict:
"python_src_tokens": list(self.python_src_tokens),
}

def syntax_highlight_text_line(self, src):
def syntax_highlight_text_line(self, src: str) -> str:
html = ""
math = False
code = False
Expand Down Expand Up @@ -553,7 +554,7 @@ def syntax_highlight_text_line(self, src):
def red_colored_span(self, innerHTML: str) -> str:
return '<span style="color:#FF5733; font-weight:bold">' + innerHTML + "</span>"

def syntax_highlight_text(self, src):
def syntax_highlight_text(self, src: str) -> str:
html = ""
lines = src.split("\n")
for line in lines:
Expand All @@ -574,7 +575,7 @@ def syntax_highlight_text(self, src):
html += "<br/>"
return html

def syntax_highlight_python(self, src):
def syntax_highlight_python(self, src: str) -> str:
lines = src.split("\n")
html = ""
for line in lines:
Expand Down

0 comments on commit c5da0c4

Please sign in to comment.