Skip to content

Commit

Permalink
Add indentation token
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWu committed Sep 26, 2018
1 parent f011d80 commit 7e26d4a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,13 @@ n 1

¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ n ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ m

### logical operators follow a comparator

#### AND

¯\\\_(ツ)\_

#### OR

¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_


### conditional statements start with ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_

¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ comparator
statement1
¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ comparator2
statement2
¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_/¯ <- optional default statement
¯\\\_(ツ)\_/¯ ¯\\\_(ツ)\_¯\\\_(ツ)\_<- optional default statement
statement2
¯\\\_(ツ)\_/¯ <- end conditional

Expand Down
6 changes: 4 additions & 2 deletions shrug_lang/shrug_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
class TokenType(Enum):
SHRUG = 1
NUMBER = 2
BOOL = 7
STRING = 3
ID = 4
EOL = 5
INVALID = 6
BOOL = 7
INDENT = 8


@dataclass(init=False)
Expand All @@ -22,7 +23,8 @@ def __init__(self, _type: TokenType, value: Union[int, str, None]=None):
if not isinstance(_type, TokenType):
raise TypeError('Token type must be of TokenType')
must_have_value = {TokenType.NUMBER, TokenType.STRING, TokenType.ID,
TokenType.INVALID, TokenType.BOOL}
TokenType.INVALID, TokenType.BOOL,
TokenType.INDENT}
if _type in must_have_value and value is None:
raise ValueError(f'Token type {_type} must have a value')
self.type = _type
Expand Down
15 changes: 13 additions & 2 deletions shrug_lang/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
class Tokenizer:
@staticmethod
def parse_line(line: str) -> List[Token]:
unparsed_tokens = filter(None, Tokenizer.join_strings(line.split(' ')))
tokens = [Tokenizer.parse_token(unparsed) for unparsed in unparsed_tokens]
unparsed_tokens = Tokenizer.join_strings(line.split(' '))
indent_size = 0
if len(unparsed_tokens) > 1:
for c in unparsed_tokens[1:]:
if c == '':
indent_size += 1
unparsed_tokens = filter(None, unparsed_tokens)
if indent_size > 0:
tokens = [Token(TokenType.INDENT, indent_size)]
else:
tokens = []
tokens += [Tokenizer.parse_token(unparsed)
for unparsed in unparsed_tokens]
tokens.append(Token(TokenType.EOL))
return tokens

Expand Down
8 changes: 4 additions & 4 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import unittest

from shrug_lang.tokenizer import Tokenizer
from shrug_lang.shrug_token import TokenType, Token
from shrug_lang.tokenizer import Tokenizer


class TestTokenizer(unittest.TestCase):
"""Line parser class should return correctly read tokens"""

def test_empty_line(self):
line = ''
empty_line = [Token(TokenType.EOL)]
self.assertEqual(empty_line, Tokenizer.parse_line(line))
self.assertEqual([Token(TokenType.EOL)], Tokenizer.parse_line(line))

line = ' '
self.assertEqual(empty_line, Tokenizer.parse_line(line))
self.assertEqual([Token(TokenType.INDENT, 5), Token(TokenType.EOL)],
Tokenizer.parse_line(line))

def test_shrug(self):
line = '¯\_(ツ)_/¯'
Expand Down

0 comments on commit 7e26d4a

Please sign in to comment.