aboutsummaryrefslogtreecommitdiff
path: root/python/generators/stdlib_docs/parse.py
blob: bdef7177388e984bcf6198b96eea8edbe4b1a4cb (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from typing import Union, List, Tuple

from python.generators.stdlib_docs import stdlib
from python.generators.stdlib_docs.utils import Errors, Pattern, get_text, fetch_comment, match_pattern


def parse_desc(docs: 'stdlib.AnyDocs') -> str:
  desc_lines = [get_text(line, False) for line in docs.desc]
  return ' '.join(desc_lines).strip('\n').strip()


# Whether comment segment about columns contain proper schema. Can be matched
# against parsed SQL data by setting `use_data_from_sql`.
def parse_columns(docs: Union['stdlib.TableViewDocs', 'stdlib.ViewFunctionDocs']
                 ) -> dict:
  cols = {}
  last_col = None
  last_desc = []
  for line in docs.columns:
    # Ignore only '--' line.
    if line == "--" or not line.startswith("-- @column"):
      last_desc.append(get_text(line))
      continue

    # Look for '-- @column' line as a column description
    m = re.match(Pattern['column'], line)
    if last_col:
      cols[last_col] = ' '.join(last_desc)
    if not m:
      print(f'Expected line {line} to match @column format', file=sys.stderr)
    last_col, last_desc = m.group(1), [m.group(2)]

  cols[last_col] = ' '.join(last_desc)
  return cols


def parse_args(docs: "stdlib.FunctionDocs") -> dict:
  if not docs.args:
    return {}

  args = {}
  last_arg, last_desc, last_type = None, [], None
  for line in docs.args:
    # Ignore only '--' line.
    if line == "--" or not line.startswith("-- @arg"):
      last_desc.append(get_text(line))
      continue

    m = re.match(Pattern['args'], line)
    if last_arg:
      args[last_arg] = {'type': last_type, 'desc': ' '.join(last_desc)}
    last_arg, last_type, last_desc = m.group(1), m.group(2), [m.group(3)]

  args[last_arg] = {'type': last_type, 'desc': ' '.join(last_desc)}
  return args


# Whether comment segment about return contain proper schema. Matches against
# parsed SQL data.
def parse_ret(docs: "stdlib.FunctionDocs") -> Tuple[str, str]:
  desc = []
  for line in docs.ret:
    # Ignore only '--' line.
    if line == "--" or not line.startswith("-- @ret"):
      desc.append(get_text(line))

    m = re.match(Pattern['return_arg'], line)
    if not m:
      print(f'Expected line {line} to match @ret format', file=sys.stderr)
    ret_type, desc = m.group(1), [m.group(2)]
  return (ret_type, ' '.join(desc))


# After matching file to Pattern, fetches and validates related documentation.
def parse_typed_docs(path: str, module: str, sql: str, Pattern: str,
                     docs_object: type
                    ) -> Tuple[List['stdlib.AnyDocs'], Errors]:
  errors = []
  line_id_to_match = match_pattern(Pattern, sql)
  lines = sql.split("\n")
  all_typed_docs = []
  for line_id, matches in line_id_to_match.items():
    # Fetch comment by looking at lines over beginning of match in reverse
    # order.
    comment = fetch_comment(lines[line_id - 1::-1])
    typed_docs, obj_errors = docs_object.create_from_comment(
        path, comment, module, matches)
    errors += obj_errors

    if not typed_docs:
      continue

    errors += typed_docs.check_comment()

    if not errors:
      all_typed_docs.append(typed_docs)

  return all_typed_docs, errors