gitee.com/yfmps/gopher-lua@v0.0.3/_tools/go-inline (about)

     1  #!/usr/bin/env python
     2  # Simple and indolent text processor to do inline go function calling
     3  
     4  import sys, re
     5  
     6  
     7  files = sys.argv
     8  class inline(object):
     9    def __init__(self, name):
    10      self.name = name
    11      self.receiver = ""
    12      self.args = []
    13      self.contents = []
    14      self.file = ""
    15      self.original = ""
    16  
    17  inlines = {}
    18  for file in sorted(files, reverse=True):
    19    contents = open(file).read().splitlines()
    20    i = 0
    21    name  = ""
    22    while i < len(contents):
    23      line = contents[i]
    24      m = re.match(".*\/\/ \+inline-start", line)
    25      if m:
    26          m2 = re.match("^func\s*(\([\*\s\w]+\))?\s*(\w+)\(([^\)]*)\)", line)
    27          name = m2.group(2)
    28          tinline = inline(name)
    29          tinline.original = line.split("//")[0].strip().rstrip("{")
    30          tinline.file = file
    31          if m2.group(1):
    32            tinline.receiver = m2.group(1).split("(")[1].split(" ")[0]
    33          tinline.args = [arg.strip().split(" ")[0] for arg in m2.group(3).split(",")]
    34          inlines[name] = tinline
    35      else:
    36        if re.match(".*\/\/\s*\+inline-end", line):
    37          inlines[name].contents = "\n".join(inlines[name].contents)
    38          name = ""
    39        elif len(name) > 0:
    40          inlines[name].contents.append(line)
    41      i += 1
    42  
    43  def do_inlining(text):
    44    contents = text.splitlines()
    45    buf = []
    46    i = 0
    47    while i < len(contents):
    48      line = contents[i]
    49      m = re.match("\s*\/\/\s*\+inline-call\s+([\w\.]+)\s+(.*)", line)
    50      if m:
    51        inlinet = inlines[m.group(1).split(".")[-1]]
    52        buf.append("// this section is inlined by go-inline")
    53        buf.append("// source function is '{}' in '{}'".format(inlinet.original, inlinet.file))
    54        buf.append("{")
    55        if len(inlinet.receiver) > 0 and inlinet.receiver != m.group(1).split(".")[0]:
    56            buf.append("{} := {}".format(inlinet.receiver, ".".join(m.group(1).split(".")[0:-1])))
    57  
    58        callargs = [arg.strip() for arg in m.group(2).split(" ")]
    59        for j in range(len(callargs)):
    60            if inlinet.args[j] != callargs[j]:
    61              buf.append("{} := {}".format(inlinet.args[j], callargs[j]))
    62        buf.append(do_inlining(inlinet.contents))
    63        buf.append("}")
    64      else:
    65        buf.append(line)
    66      i += 1
    67    return "\n".join(buf)
    68  
    69  for file in files:
    70    if not file.startswith("_"):
    71      continue
    72    contents = open(file).read()
    73    with open(file.lstrip("_"), "w") as io:
    74      inlined = do_inlining(contents).split("\n")
    75      for i in range(len(inlined)):
    76        if i == 1:
    77          io.write("////////////////////////////////////////////////////////\n")
    78          io.write("// This file was generated by go-inline. DO NOT EDIT. //\n")
    79          io.write("////////////////////////////////////////////////////////\n")
    80        io.write(inlined[i])
    81        io.write("\n")
    82      io.write("\n")