ply文件 java_ply之解析java文件,找出包名、类名、依赖类

[Python] 纯文本查看 复制代码tokens = (

'PACKAGE',

'PACKAGENAME',

'COMMA',

'ID',

'PUBLIC',

'CLASS',

'LBRACE',

'INTERFACE',

'EXTENDS',

'IMPLEMENTS',

'PACKAGEPATH',

'IMPORT'

)

states = (

('multicomment','exclusive'),

('package','exclusive'),

)

reserved = {

'public' : 'PUBLIC',

'class' : 'CLASS',

'extends' : 'EXTENDS',

'implements' : 'IMPLEMENTS',

'interface' : 'INTERFACE',

'import' : 'IMPORT'

}

def t_LBRACE(t):

r'\{'

return t

def t_COMMENT(t):

r'\/\/.*'

def t_PACKAGE(t):

r'\bpackage\b'

t.lexer.begin('package')

return t

def t_package_PACKAGENAME(t):

r'([a-zA-Z_$][a-zA-Z_$0-9]*)(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*'

return t

t_package_ignore = " \t"

def t_package_COMMA(t):

r';'

t.lexer.begin('INITIAL')

return t

def t_package_error(t):

print("Illegal character '%s'" % t.value[0])

t.lexer.skip(ply_java_filelen)

def t_LCOMMENT(t):

r'\/\*'

t.lexer.begin('multicomment')

def t_multicomment_newline(t):

r'[\r\n]+'

def t_multicomment_RCOMMENT(t):

r'\*\/'

t.lexer.begin('INITIAL')

def t_multicomment_MCOMMENT(t):

r'((?!\*\/).)+'

def t_multicomment_error(t):

print("Illegal character '%s'" % t.value[0])

t.lexer.skip(ply_java_filelen)

t_multicomment_ignore = ""

def t_PACKAGEPATH(t):

r'([a-zA-Z_$][a-zA-Z_$0-9]*)(\.[a-zA-Z_$][a-zA-Z_$0-9]*)+'

return t

def t_ID(t):

r'[a-zA-Z_$][a-zA-Z_$0-9]*'

t.type = reserved.get(t.value,'ID') # Check for reserved words

return t

def t_COMMA(t):

r';'

return t

def t_newline(t):

r'[\r\n]+'

def t_error(t):

#print("Illegal character '%s'" % t.value[0])

t.lexer.skip(ply_java_filelen)

# Ignored characters

t_ignore = " \t"

def p_error(t):

#print("Syntax error at '%s'" % t.value)

pass

def p_prog_java(t):

'''prog : pkgline importlines decors PUBLIC decors CLASS ID extend PACKAGEPATH LBRACE

| pkgline importlines decors PUBLIC decors INTERFACE ID extend PACKAGEPATH LBRACE

| pkgline importlines decors PUBLIC decors CLASS ID extend ID LBRACE

| pkgline importlines decors PUBLIC decors INTERFACE ID extend ID LBRACE

| pkgline importlines decors PUBLIC decors CLASS ID LBRACE

| pkgline importlines decors PUBLIC decors INTERFACE ID LBRACE'''

global ply_java_classname

ply_java_classname = t[7]

def p_importlines_java(t):

'''importlines : importline

| '''

def p_importline_java(t):

'''importline : importline import

| import'''

def p_import_java(t):

'import : IMPORT PACKAGEPATH COMMA'

global ply_java_depends

ply_java_depends.append(t[2])

def p_pkgline_java(t):

"pkgline : PACKAGE pkgname COMMA"

global ply_java_pkgname

ply_java_pkgname = t[2]

def p_extend_java(t):

'''extend : EXTENDS

| IMPLEMENTS'''

def p_pkgname_java(t):

'pkgname : PACKAGENAME'

t[0] = t[1]

def p_decors_java(t):

'''decors : decor

| '''

def p_decor_java(t):

'''decor : decor ID

| ID'''

ply_java_filelen = 0

ply_java_pkgname = ''

ply_java_classname = ''

ply_java_depends = []

import ply.lex as lex

lexer = lex.lex()

import ply.yacc as yacc

parser = yacc.yacc()

def analyzeJava(fname):

global ply_java_pkgname,ply_java_classname,ply_java_depends,ply_java_filelen

ply_java_pkgname = ""

ply_java_classname = ""

ply_java_depends.clear()

ply_java_filelen = 0

try:

f = open(fname,"r")

f.seek(0,2)

ply_java_filelen = f.tell()

f.seek(0,0)

buffer = f.read(ply_java_filelen)

f.close()

except Exception as e:

print(str(e))

parser.parse(buffer)

return {'pkgname':ply_java_pkgname,'classname':ply_java_classname,'depends':ply_java_depends}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值