11import os
22
3- # this is the universal token, used by all lexers to know what to output
4- from lexers .token import TokenType
5-
6- # these are the individual lexers for all languages we support
7- from lexers .ruby .rubylexer import RubyLexer
8- from lexers .python .pythonlexer import PythonLexer
9- from lexers .javascript .javascriptlexer import JavaScriptLexer
10- from lexers .golang .golexer import GoLexer
11-
123# gustavo testando alguma coisa
4+ from spice .analyzers .identation import detect_indentation
5+
136
14- from spice .identation import detect_indentation
15-
16- # this will read the file extension and return the correct lexer
17- def get_lexer_for_file (file_path ):
18- _ , ext = os .path .splitext (file_path )
19-
20- if ext == ".rb" :
21- return RubyLexer
22- elif ext == ".py" :
23- return PythonLexer
24- elif ext == ".js" :
25- return JavaScriptLexer
26- elif ext == ".go" :
27- return GoLexer
28- else :
29- raise ValueError (f"Unsupported file extension: { ext } " )
30-
317
328# this is the analyze function
339def analyze_file (file_path : str , selected_stats = None ):
@@ -56,21 +32,31 @@ def analyze_file(file_path: str, selected_stats=None):
5632
5733 # line count if requested
5834 if "line_count" in selected_stats :
35+ from spice .analyzers .count_lines import count_lines
5936 results ["line_count" ] = count_lines (code )
37+
38+ # comment line count if requested
39+ if "comment_line_count" in selected_stats :
40+ from spice .analyzers .count_comment_lines import count_comment_lines
41+ results ["comment_line_count" ] = count_comment_lines (code )
42+
43+ # @gtins botei sua funcao aqui pq ela usa o codigo raw e nao o tokenizado, ai so tirei ela ali de baixo pra nao ficar chamando o parser sem precisar
44+ # edit: ok i see whats going on, instead of appending the results to the resuls, this will itself print the results to the terminal
45+ # TODO: make analyze_code_structure return the results, then append those results to the results array
46+ if "identation_level" in selected_stats :
47+ analyze_code_structure (code )
6048
61- # only put the code through the lexer and proceed with tokenization if we need function count or comment count (UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!)
62- if "function_count" in selected_stats or "comment_line_count" in selected_stats :
49+ # only put the code through the lexer and proceed with tokenization if we need function count (UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!)
50+ if "function_count" in selected_stats :
51+
6352 # get the lexer for the code's language
53+ from spice .utils .get_lexer import get_lexer_for_file
6454 LexerClass = get_lexer_for_file (file_path )
6555
6656 # tokenize the code via lexer
6757 lexer = LexerClass (code )
6858 tokens = lexer .tokenize ()
6959
70- # process comment line count if requested
71- if "comment_line_count" in selected_stats :
72- results ["comment_line_count" ] = count_comment_lines (code )
73-
7460 # only put the code through the parser and proceed with parsing if we need function count (UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!)
7561 if "function_count" in selected_stats :
7662
@@ -82,83 +68,20 @@ def analyze_file(file_path: str, selected_stats=None):
8268 ast = parser .parse ()
8369
8470 # count functions
71+ from spice .analyzers .count_functions import count_functions
8572 results ["function_count" ] = count_functions (ast )
86- if "identation_level" in selected_stats :
87- analyze_code_structure (code )
8873
8974 return results
9075
9176
92- # this will count lines straight from the raw code
93- def count_lines (code ):
94- return code .count ("\n " ) + 1
95-
96-
97- # this will count functions in the AST
98- def count_functions (ast ):
99- # import function definition from the parser's ast
100- from parser .ast import FunctionDefinition , Program
101-
102- if not isinstance (ast , Program ):
103- return 0
104-
105- function_count = 0
106-
107- # recursive search for function definitions in the AST
108- def search_node (node ):
109- nonlocal function_count
110-
111- if isinstance (node , FunctionDefinition ):
112- function_count += 1
113-
114- # process child nodes if they exist
115- if hasattr (node , 'statements' ) and node .statements :
116- for statement in node .statements :
117- search_node (statement )
118-
119- if hasattr (node , 'body' ) and node .body :
120- for body_statement in node .body :
121- search_node (body_statement )
122-
123- # for binary operation, check both sides
124- if hasattr (node , 'left' ):
125- search_node (node .left )
126- if hasattr (node , 'right' ):
127- search_node (node .right )
128-
129- # check the value part of an assignment
130- if hasattr (node , 'value' ):
131- search_node (node .value )
132-
133- # check function call arguments
134- if hasattr (node , 'arguments' ) and node .arguments :
135- for arg in node .arguments :
136- search_node (arg )
137-
138- # start recursive search from the root Program node
139- search_node (ast )
140-
141- return function_count
14277
14378
144- # this will count comment lines, since our AST/Parser doesn't include comment lines, this needs to be done in the tokenized output of the lexer
145- # COMMENT LINE IS A LINE THAT EXCLUSIVELY HAS A COMMENT
146- # so like: y = 5 #sets y to 5 IS NOT A COMMENT LINE!!!!!!!!
147- def count_comment_lines (code ):
148- """Count lines that are exclusively comments (no code on the same line)"""
149- # split the code into lines
150- lines = code .splitlines ()
151- comment_count = 0
152-
153- for line in lines :
154- # Remove leading whitespace
155- stripped = line .strip ()
156- # Check if this line consists only of a comment
157- if stripped and stripped .startswith ('#' ):
158- comment_count += 1
159-
160- return comment_count
16179
80+ # im not sure what to do with this part 😂
81+ # this is the identation analyzer
82+ # but it's not included in the menu?
83+ # im not going to change this since gtins knows better than me how this works
84+ # but this needs to be refactores and included directly into the analyze_file function and the analyze menu
16285def analyze_code_structure (code ):
16386 indentation_info = detect_indentation (code )
16487
@@ -167,4 +90,6 @@ def analyze_code_structure(code):
16790 for line , level in indentation_info ["levels" ]:
16891 # print(f"Indentation Level {level}: {line}")
16992 print (f"Detected Indentation Type: { indentation_info ['indent_type' ]} " )
170- print (f"Detected Indentation Size: { indentation_info ['indent_size' ]} " )
93+ print (f"Detected Indentation Size: { indentation_info ['indent_size' ]} " )
94+
95+ # ----------------------------------------------------------------------------------------------------
0 commit comments