Skip to content

Commit f9cdbc5

Browse files
mchehabJonathan Corbet
authored andcommitted
scripts/lib/kdoc/kdoc_parser.py: move states to a separate class
States are really enums. on Python, enums are actually classes, as can be seen at: https://docs.python.org/3/library/enum.html Yet, I can't see any advantage of derivating the class from enum class here. So, just place the states on a separate class. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net> Link: https://lore.kernel.org/r/00cb4e0b8a1545bf7c4401b58213841db5cba2e2.1744685912.git.mchehab+huawei@kernel.org
1 parent 439111e commit f9cdbc5

1 file changed

Lines changed: 64 additions & 59 deletions

File tree

scripts/lib/kdoc/kdoc_parser.py

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,22 @@
6161

6262
type_param = KernRe(r"\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)", cache=False)
6363

64-
65-
class KernelDoc:
64+
class state:
6665
"""
67-
Read a C language source or header FILE and extract embedded
68-
documentation comments.
66+
State machine enums
6967
"""
7068

7169
# Parser states
72-
STATE_NORMAL = 0 # normal code
73-
STATE_NAME = 1 # looking for function name
74-
STATE_BODY_MAYBE = 2 # body - or maybe more description
75-
STATE_BODY = 3 # the body of the comment
76-
STATE_BODY_WITH_BLANK_LINE = 4 # the body which has a blank line
77-
STATE_PROTO = 5 # scanning prototype
78-
STATE_DOCBLOCK = 6 # documentation block
79-
STATE_INLINE = 7 # gathering doc outside main block
80-
81-
st_name = [
70+
NORMAL = 0 # normal code
71+
NAME = 1 # looking for function name
72+
BODY_MAYBE = 2 # body - or maybe more description
73+
BODY = 3 # the body of the comment
74+
BODY_WITH_BLANK_LINE = 4 # the body which has a blank line
75+
PROTO = 5 # scanning prototype
76+
DOCBLOCK = 6 # documentation block
77+
INLINE = 7 # gathering doc outside main block
78+
79+
name = [
8280
"NORMAL",
8381
"NAME",
8482
"BODY_MAYBE",
@@ -90,22 +88,29 @@ class KernelDoc:
9088
]
9189

9290
# Inline documentation state
93-
STATE_INLINE_NA = 0 # not applicable ($state != STATE_INLINE)
94-
STATE_INLINE_NAME = 1 # looking for member name (@foo:)
95-
STATE_INLINE_TEXT = 2 # looking for member documentation
96-
STATE_INLINE_END = 3 # done
97-
STATE_INLINE_ERROR = 4 # error - Comment without header was found.
98-
# Spit a warning as it's not
99-
# proper kernel-doc and ignore the rest.
100-
101-
st_inline_name = [
91+
INLINE_NA = 0 # not applicable ($state != INLINE)
92+
INLINE_NAME = 1 # looking for member name (@foo:)
93+
INLINE_TEXT = 2 # looking for member documentation
94+
INLINE_END = 3 # done
95+
INLINE_ERROR = 4 # error - Comment without header was found.
96+
# Spit a warning as it's not
97+
# proper kernel-doc and ignore the rest.
98+
99+
inline_name = [
102100
"",
103101
"_NAME",
104102
"_TEXT",
105103
"_END",
106104
"_ERROR",
107105
]
108106

107+
108+
class KernelDoc:
109+
"""
110+
Read a C language source or header FILE and extract embedded
111+
documentation comments.
112+
"""
113+
109114
# Section names
110115

111116
section_default = "Description" # default section
@@ -122,8 +127,8 @@ def __init__(self, config, fname):
122127
self.config = config
123128

124129
# Initial state for the state machines
125-
self.state = self.STATE_NORMAL
126-
self.inline_doc_state = self.STATE_INLINE_NA
130+
self.state = state.NORMAL
131+
self.inline_doc_state = state.INLINE_NA
127132

128133
# Store entry currently being processed
129134
self.entry = None
@@ -260,8 +265,8 @@ def reset_state(self, ln):
260265
self.entry.leading_space = None
261266

262267
# State flags
263-
self.state = self.STATE_NORMAL
264-
self.inline_doc_state = self.STATE_INLINE_NA
268+
self.state = state.NORMAL
269+
self.inline_doc_state = state.INLINE_NA
265270
self.entry.brcount = 0
266271

267272
self.entry.in_doc_sect = False
@@ -1166,7 +1171,7 @@ def process_normal(self, ln, line):
11661171
self.entry.in_doc_sect = False
11671172

11681173
# next line is always the function name
1169-
self.state = self.STATE_NAME
1174+
self.state = state.NAME
11701175

11711176
def process_name(self, ln, line):
11721177
"""
@@ -1182,7 +1187,7 @@ def process_name(self, ln, line):
11821187
self.entry.section = doc_block.group(1)
11831188

11841189
self.entry.identifier = self.entry.section
1185-
self.state = self.STATE_DOCBLOCK
1190+
self.state = state.DOCBLOCK
11861191
return
11871192

11881193
if doc_decl.search(line):
@@ -1224,7 +1229,7 @@ def process_name(self, ln, line):
12241229

12251230
self.entry.identifier = self.entry.identifier.strip(" ")
12261231

1227-
self.state = self.STATE_BODY
1232+
self.state = state.BODY
12281233

12291234
# if there's no @param blocks need to set up default section here
12301235
self.entry.section = self.section_default
@@ -1238,14 +1243,14 @@ def process_name(self, ln, line):
12381243
r = KernRe(r"\s+")
12391244
self.entry.descr = r.sub(" ", self.entry.descr)
12401245
self.entry.declaration_purpose = self.entry.descr
1241-
self.state = self.STATE_BODY_MAYBE
1246+
self.state = state.BODY_MAYBE
12421247
else:
12431248
self.entry.declaration_purpose = ""
12441249

12451250
if not self.entry.is_kernel_comment:
12461251
self.emit_warning(ln,
12471252
f"This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst\n{line}")
1248-
self.state = self.STATE_NORMAL
1253+
self.state = state.NORMAL
12491254

12501255
if not self.entry.declaration_purpose and self.config.wshort_desc:
12511256
self.emit_warning(ln,
@@ -1254,7 +1259,7 @@ def process_name(self, ln, line):
12541259
if not self.entry.identifier and self.entry.decl_type != "enum":
12551260
self.emit_warning(ln,
12561261
f"wrong kernel-doc identifier on line:\n{line}")
1257-
self.state = self.STATE_NORMAL
1262+
self.state = state.NORMAL
12581263

12591264
if self.config.verbose:
12601265
self.emit_warning(ln,
@@ -1271,7 +1276,7 @@ def process_body(self, ln, line):
12711276
STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
12721277
"""
12731278

1274-
if self.state == self.STATE_BODY_WITH_BLANK_LINE:
1279+
if self.state == state.BODY_WITH_BLANK_LINE:
12751280
r = KernRe(r"\s*\*\s?\S")
12761281
if r.match(line):
12771282
self.dump_section()
@@ -1311,7 +1316,7 @@ def process_body(self, ln, line):
13111316
if self.entry.contents:
13121317
self.entry.contents += "\n"
13131318

1314-
self.state = self.STATE_BODY
1319+
self.state = state.BODY
13151320
return
13161321

13171322
if doc_end.search(line):
@@ -1325,7 +1330,7 @@ def process_body(self, ln, line):
13251330
self.entry.prototype = ""
13261331
self.entry.new_start_line = ln + 1
13271332

1328-
self.state = self.STATE_PROTO
1333+
self.state = state.PROTO
13291334
return
13301335

13311336
if doc_content.search(line):
@@ -1336,16 +1341,16 @@ def process_body(self, ln, line):
13361341
self.dump_section()
13371342

13381343
self.entry.new_start_line = ln
1339-
self.state = self.STATE_BODY
1344+
self.state = state.BODY
13401345
else:
13411346
if self.entry.section != self.section_default:
1342-
self.state = self.STATE_BODY_WITH_BLANK_LINE
1347+
self.state = state.BODY_WITH_BLANK_LINE
13431348
else:
1344-
self.state = self.STATE_BODY
1349+
self.state = state.BODY
13451350

13461351
self.entry.contents += "\n"
13471352

1348-
elif self.state == self.STATE_BODY_MAYBE:
1353+
elif self.state == state.BODY_MAYBE:
13491354

13501355
# Continued declaration purpose
13511356
self.entry.declaration_purpose = self.entry.declaration_purpose.rstrip()
@@ -1388,7 +1393,7 @@ def process_body(self, ln, line):
13881393
def process_inline(self, ln, line):
13891394
"""STATE_INLINE: docbook comments within a prototype."""
13901395

1391-
if self.inline_doc_state == self.STATE_INLINE_NAME and \
1396+
if self.inline_doc_state == state.INLINE_NAME and \
13921397
doc_inline_sect.search(line):
13931398
self.entry.section = doc_inline_sect.group(1)
13941399
self.entry.new_start_line = ln
@@ -1397,29 +1402,29 @@ def process_inline(self, ln, line):
13971402
if self.entry.contents != "":
13981403
self.entry.contents += "\n"
13991404

1400-
self.inline_doc_state = self.STATE_INLINE_TEXT
1405+
self.inline_doc_state = state.INLINE_TEXT
14011406
# Documentation block end */
14021407
return
14031408

14041409
if doc_inline_end.search(line):
14051410
if self.entry.contents not in ["", "\n"]:
14061411
self.dump_section()
14071412

1408-
self.state = self.STATE_PROTO
1409-
self.inline_doc_state = self.STATE_INLINE_NA
1413+
self.state = state.PROTO
1414+
self.inline_doc_state = state.INLINE_NA
14101415
return
14111416

14121417
if doc_content.search(line):
1413-
if self.inline_doc_state == self.STATE_INLINE_TEXT:
1418+
if self.inline_doc_state == state.INLINE_TEXT:
14141419
self.entry.contents += doc_content.group(1) + "\n"
14151420
if not self.entry.contents.strip(" ").rstrip("\n"):
14161421
self.entry.contents = ""
14171422

1418-
elif self.inline_doc_state == self.STATE_INLINE_NAME:
1423+
elif self.inline_doc_state == state.INLINE_NAME:
14191424
self.emit_warning(ln,
14201425
f"Incorrect use of kernel-doc format: {line}")
14211426

1422-
self.inline_doc_state = self.STATE_INLINE_ERROR
1427+
self.inline_doc_state = state.INLINE_ERROR
14231428

14241429
def syscall_munge(self, ln, proto): # pylint: disable=W0613
14251430
"""
@@ -1598,8 +1603,8 @@ def process_proto(self, ln, line):
15981603
self.dump_section(start_new=False)
15991604

16001605
elif doc_inline_start.search(line):
1601-
self.state = self.STATE_INLINE
1602-
self.inline_doc_state = self.STATE_INLINE_NAME
1606+
self.state = state.INLINE
1607+
self.inline_doc_state = state.INLINE_NAME
16031608

16041609
elif self.entry.decl_type == 'function':
16051610
self.process_proto_function(ln, line)
@@ -1663,7 +1668,7 @@ def parse_kdoc(self):
16631668
line = line.expandtabs().strip("\n")
16641669

16651670
# Group continuation lines on prototypes
1666-
if self.state == self.STATE_PROTO:
1671+
if self.state == state.PROTO:
16671672
if line.endswith("\\"):
16681673
prev += line.rstrip("\\")
16691674
cont = True
@@ -1681,8 +1686,8 @@ def parse_kdoc(self):
16811686
prev_ln = None
16821687

16831688
self.config.log.debug("%d %s%s: %s",
1684-
ln, self.st_name[self.state],
1685-
self.st_inline_name[self.inline_doc_state],
1689+
ln, state.name[self.state],
1690+
state.inline_name[self.inline_doc_state],
16861691
line)
16871692

16881693
# This is an optimization over the original script.
@@ -1696,18 +1701,18 @@ def parse_kdoc(self):
16961701
self.process_export(export_table, line)
16971702

16981703
# Hand this line to the appropriate state handler
1699-
if self.state == self.STATE_NORMAL:
1704+
if self.state == state.NORMAL:
17001705
self.process_normal(ln, line)
1701-
elif self.state == self.STATE_NAME:
1706+
elif self.state == state.NAME:
17021707
self.process_name(ln, line)
1703-
elif self.state in [self.STATE_BODY, self.STATE_BODY_MAYBE,
1704-
self.STATE_BODY_WITH_BLANK_LINE]:
1708+
elif self.state in [state.BODY, state.BODY_MAYBE,
1709+
state.BODY_WITH_BLANK_LINE]:
17051710
self.process_body(ln, line)
1706-
elif self.state == self.STATE_INLINE: # scanning for inline parameters
1711+
elif self.state == state.INLINE: # scanning for inline parameters
17071712
self.process_inline(ln, line)
1708-
elif self.state == self.STATE_PROTO:
1713+
elif self.state == state.PROTO:
17091714
self.process_proto(ln, line)
1710-
elif self.state == self.STATE_DOCBLOCK:
1715+
elif self.state == state.DOCBLOCK:
17111716
self.process_docblock(ln, line)
17121717
except OSError:
17131718
self.config.log.error(f"Error: Cannot open file {self.fname}")

0 commit comments

Comments
 (0)