-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·98 lines (74 loc) · 2.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
#
# Trivial driver-script which will parse a simple "PHP-script", outputting
# only the two token-types we know about:
#
# 1. Comments.
#
# 2. Class definitions.
#
import lexer
input = """
/* Class Bar is known for serving alcohol. */
class Bar {
}
/* Class Foo reminds us that soft-drinks are also fine. */
class Foo extends Bar {
/* PHP code goes here */
// Comment
super!
}
/* Class Foo reminds us that soft-drinks are also fine. */
class Baz extends Bar {
// Oops I renamed here. Copy pasting is bad!
}
/* Trailing comment - I'm not even closed!
"""
#
# Create a lexer object, giving it the input above
#
l = lexer.Lexer(input)
#
# Lex it
#
out = l.Lex()
#
# Show what we did found here.
#
for x in out:
print( x )
print( "\n\n\n")
print( "Pretend we're linting now\n\n\n")
#
# If you wanted to look for class mismatches here's a trivial way
# of doing it
#
# NOTE: Here we start at the second index in our tokens. This is fine,
# if the first entry is a class then there cannot be a comment ahead of
# it. If the first entry is a comment it will be tested if the second is
# a class.
#
# We're just doing this to allow us to refer to the previous-token easily.
#
i = 1
while i < len(out):
# Is this a class definition?
if out[i]['type'] == 'class':
# Get the name
clazz = out[i]['name']
print("Found class with name " + clazz)
# If the previous thing is a comment
if out[i-1]['type'] == 'comment':
print(" The previous token was a comment. Yay!")
comment = out[i-1]['value']
# And it mentions "class ..." then we'll test to see if
# it has the name of the class in it
if "class " in comment.lower():
print( " The comment refers to a class ..")
if clazz in comment:
print( " OK : The comment refers to the expected class!")
else:
print( " FAIL: Comment does not refer to the expected class: " + clazz)
else:
print( " The comment does not refer to a class")
i += 1