正则表达式匹配代码块的注释

使用正则内部的逻辑短路匹配之一举多得

如果只匹配一个注释的话:

要判断它在不在字符串内:向前统计引号个数???
是不是正则的一部分:只存在 reg =/\// 这种情况,但(?>!)的兼容性貌似不是很好

只需要给出字符串、注释、正则表达式 基本构成条件就不会出现上面两个问题

regexp = /str|regexp|lineannotaion|blockannotation|tampstring/g

let tmp='', str = `
    ${
     tmp+'\`'+"${
     }"+
    `${
   tmp}`+ /*A*/
    (tmp={'a':/(?<!x`)/g}["a"]) //B
}'c' \" <d> & //  e /*f*/`,  //  g
reg = /(?<!\\)\/(?![^\/\*])[\s\S]+?(?<!\\)\//g;
//*/ h
    str; reg = /c/g, str = ``
/*/  j
    k //KK
//*/
str = "\"http://'xxxx'\/\"";
str = `.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ')${
     `l`+str}`
str = '\
   \' //n"`\
' /* // */

匹配任意字符

/[\s\S]/g
/[^]/g

匹配单行注释

/\/\/.*/g

匹配多行注释

/\/\*[^]*?\*\//g

匹配字符串

/('|"|`)[^]*?(?<!\\)\1/g
/''|``|""|('|"|`)[^]*?[^\\]\1/g
/''|""|('|")[^]*?[^\\]\1|`(?:\\[\s\S]|\${
     (?:[^{
     }<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较复杂的任务,需要实现很多功能,我们可以逐步实现。首先,我们打开指定的文件并读取全部内容,并使用正则表达式去除注释,并按行分割并去除空白字符。代码如下: ```python import re def read_verilog_file(filepath): with open(filepath, 'r') as f: content = f.read() # remove comments content = re.sub(r'//.*', '', content) content = re.sub(r'/\*(.|\n)*?\*/', '', content) # split into lines and remove white spaces lines = [line.strip() for line in content.split('\n')] return lines ``` 接下来,我们需要实现统计不在模块定义、包定义、函数定义、任务定义、generate块等特殊语句内的代码行数。这个可以通过遍历每一行代码,使用正则表达式匹配特殊语句,并判断当前是否在特殊语句内实现。代码如下: ```python def count_code_lines(lines): special_keywords = ['module', 'macromodule', 'package', 'function', 'task', 'generate'] code_lines = 0 in_special_block = False special_block_keyword = '' for line in lines: if in_special_block: if line.strip().startswith('end' + special_block_keyword): in_special_block = False special_block_keyword = '' continue for keyword in special_keywords: if line.strip().startswith(keyword): in_special_block = True special_block_keyword = keyword break else: if line.strip(): code_lines += 1 return code_lines ``` 然后,我们需要查找每个模块实例化语句,并递归计算每个模块的行数,将其加入总行数中。这个可以通过正则表达式匹配模块实例化语句,然后递归计算每个模块的行数。代码如下: ```python def count_module_lines(lines): module_lines = {} module_regex = re.compile(r'^\s*(\w+)\s+(\w+)\s*\(') for line in lines: match = module_regex.match(line) if match: module_name = match.group(1) instance_name = match.group(2) module_file = module_name + '.v' module_file_lines = read_verilog_file(module_file) module_lines[instance_name] = count_lines(module_file_lines) return module_lines ``` 最后,我们需要查找每个 include 语句,并递归计算每个 included 文件的行数,将其加入总行数中。这个可以通过正则表达式匹配 include 语句,然后递归计算每个 included 文件的行数。代码如下: ```python def count_include_lines(lines): include_lines = {} include_regex = re.compile(r'^\s*`include\s+"(.+)"') for line in lines: match = include_regex.match(line) if match: include_file = match.group(1) include_file_lines = read_verilog_file(include_file) include_lines[include_file] = count_lines(include_file_lines) return include_lines ``` 最后,我们将这些功能整合起来,输出结果。代码如下: ```python def count_verilog_lines(filepath): lines = read_verilog_file(filepath) code_lines = count_code_lines(lines) module_lines = count_module_lines(lines) include_lines = count_include_lines(lines) print('Total code lines:', code_lines) print('Module lines:') for instance_name, lines in module_lines.items(): print(' {}: {}'.format(instance_name, lines)) print('Include lines:') for include_file, lines in include_lines.items(): print(' {}: {}'.format(include_file, lines)) ``` 这样,我们就完成了一个统计Verilog代码行数的脚本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值