-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
30 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
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,38 @@ | ||
''' | ||
Language parser for Go lang | ||
''' | ||
|
||
from .code_reader import CodeStateMachine | ||
|
||
|
||
class GoLikeStates(CodeStateMachine): # pylint: disable=R0903 | ||
|
||
FUNC_KEYWORD = 'func' | ||
|
||
def _state_global(self, token): | ||
if token == self.FUNC_KEYWORD: | ||
self._state = self._function_name | ||
self.context.start_new_function('') | ||
|
||
def _function_name(self, token): | ||
if token == '(': | ||
return self.next(self._member_function, token) | ||
self.context.add_to_function_name(token) | ||
self._state = self._function_dec | ||
|
||
@CodeStateMachine.read_inside_brackets_then("()", '_function_name') | ||
def _member_function(self, tokens): | ||
self.context.add_to_long_function_name(tokens) | ||
|
||
@CodeStateMachine.read_inside_brackets_then("()", '_expect_function_impl') | ||
def _function_dec(self, token): | ||
if token not in '()': | ||
self.context.parameter(token) | ||
|
||
def _expect_function_impl(self, token): | ||
self.next_if(self._function_impl, token, '{') | ||
|
||
@CodeStateMachine.read_inside_brackets_then("{}") | ||
def _function_impl(self, _): | ||
self._state = self._state_global | ||
self.context.end_of_function() |
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,24 @@ | ||
''' | ||
Language parser for Go lang | ||
''' | ||
|
||
from .code_reader import CodeReader | ||
from .clike import CCppCommentsMixin | ||
from .golike import GoLikeStates | ||
|
||
|
||
class RustReader(CodeReader, CCppCommentsMixin): | ||
# pylint: disable=R0903 | ||
|
||
ext = ['rs'] | ||
language_names = ['rust'] | ||
_conditions = set(['if', 'for', 'while', '&&', '||', '?', 'catch', | ||
'case', 'match']) | ||
|
||
def __init__(self, context): | ||
super(RustReader, self).__init__(context) | ||
self.parallel_states = [RustStates(context)] | ||
|
||
|
||
class RustStates(GoLikeStates): # pylint: disable=R0903 | ||
FUNC_KEYWORD = 'fn' |
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,41 @@ | ||
import unittest | ||
from lizard import analyze_file, FileAnalyzer, get_extensions | ||
|
||
|
||
def get_rust_fileinfo(source_code): | ||
return analyze_file.analyze_source_code("a.rs", source_code) | ||
|
||
|
||
def get_rust_function_list(source_code): | ||
return get_rust_fileinfo(source_code).function_list | ||
|
||
|
||
class TestRust(unittest.TestCase): | ||
|
||
def test_main(self): | ||
result = get_rust_function_list(''' | ||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
''') | ||
self.assertEqual(1, len(result)) | ||
self.assertEqual('main', result[0].name) | ||
|
||
def test_return(self): | ||
result = get_rust_function_list(''' | ||
fn plus_one(x: i32) -> i32 { | ||
x + 1; | ||
} | ||
''') | ||
self.assertEqual(1, len(result)) | ||
self.assertEqual('plus_one', result[0].name) | ||
|
||
def test_if(self): | ||
result = get_rust_function_list(''' | ||
fn main() { | ||
match a() {} | ||
} | ||
''') | ||
self.assertEqual(1, len(result)) | ||
self.assertEqual(2, result[0].cyclomatic_complexity) | ||
|
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,3 @@ | ||
## ES6 | ||
|
||
Support interpolating in string literal. |