-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding code to support tokenizing the line: 1) Added token attribute to the line object 2) Added token.py file 3) Adding tests Currently the tokenizer handles consecutive spaces, comments and characters. NOTE: Future commits will handle VHDL symbols.
- Loading branch information
1 parent
865af46
commit e3f6263
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
import unittest | ||
|
||
from vsg import line | ||
|
||
class testTokenMethod(unittest.TestCase): | ||
|
||
def test_token_create_method_with_single_spaces(self): | ||
oLine = line.line('contents of line') | ||
self.assertTrue(oLine) | ||
self.assertEqual('contents of line', oLine.line) | ||
|
||
lTokens = [] | ||
lTokens.append('contents') | ||
lTokens.append(' ') | ||
lTokens.append('of') | ||
lTokens.append(' ') | ||
lTokens.append('line') | ||
|
||
self.assertEqual(lTokens, oLine.tokens) | ||
|
||
def test_token_create_method_with_multiple_spaces(self): | ||
oLine = line.line(' contents of line') | ||
self.assertTrue(oLine) | ||
self.assertEqual(' contents of line', oLine.line) | ||
|
||
lTokens = [] | ||
lTokens.append(' ') | ||
lTokens.append('contents') | ||
lTokens.append(' ') | ||
lTokens.append('of') | ||
lTokens.append(' ') | ||
lTokens.append('line') | ||
|
||
self.assertEqual(lTokens, oLine.tokens) | ||
|
||
def test_token_create_method_with_comment_at_end_of_line_without_spaces_around_dashes(self): | ||
oLine = line.line('contents of line--This is a comment') | ||
self.assertTrue(oLine) | ||
self.assertEqual('contents of line--This is a comment', oLine.line) | ||
|
||
lTokens = [] | ||
lTokens.append('contents') | ||
lTokens.append(' ') | ||
lTokens.append('of') | ||
lTokens.append(' ') | ||
lTokens.append('line') | ||
lTokens.append('--This is a comment') | ||
|
||
self.assertEqual(lTokens, oLine.tokens) | ||
|
||
def test_token_create_method_with_comment_at_end_of_line_with_spaces_around_dashes(self): | ||
oLine = line.line('contents of line -- This is a comment') | ||
self.assertTrue(oLine) | ||
self.assertEqual('contents of line -- This is a comment', oLine.line) | ||
|
||
lTokens = [] | ||
lTokens.append('contents') | ||
lTokens.append(' ') | ||
lTokens.append('of') | ||
lTokens.append(' ') | ||
lTokens.append('line') | ||
lTokens.append(' ') | ||
lTokens.append('-- This is a comment') | ||
|
||
self.assertEqual(lTokens, oLine.tokens) | ||
|
||
def test_token_create_method_with_comment_at_end_of_line_with_spaces_at_the_end_of_line(self): | ||
oLine = line.line('contents of line -- This is a comment ') | ||
self.assertTrue(oLine) | ||
self.assertEqual('contents of line -- This is a comment ', oLine.line) | ||
|
||
lTokens = [] | ||
lTokens.append('contents') | ||
lTokens.append(' ') | ||
lTokens.append('of') | ||
lTokens.append(' ') | ||
lTokens.append('line') | ||
lTokens.append(' ') | ||
lTokens.append('-- This is a comment ') | ||
|
||
self.assertEqual(lTokens, oLine.tokens) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
def create(sString): | ||
''' | ||
This function takes a string and returns a list of tokens. | ||
''' | ||
lReturn = [] | ||
sToken = '' | ||
fCommentFound = False | ||
for iIndex, sChar in enumerate(sString): | ||
if len(sToken) == 0: | ||
sToken = sChar | ||
else: | ||
if sChar == '-' and sString[iIndex + 1] == '-': | ||
fCommentFound = True | ||
lReturn.append(sToken) | ||
sToken = '' | ||
if fCommentFound: | ||
sToken += sChar | ||
continue | ||
if sChar == ' ' and sToken[-1] == ' ': | ||
sToken += sChar | ||
if not sChar == ' ' and not sToken[-1] == ' ': | ||
sToken += sChar | ||
if sChar == ' ' and not sToken[-1] == ' ': | ||
lReturn.append(sToken) | ||
sToken = sChar | ||
if not sChar == ' ' and sToken[-1] == ' ': | ||
lReturn.append(sToken) | ||
sToken = sChar | ||
|
||
lReturn.append(sToken) | ||
return lReturn |