Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
a14f0232
提交
a14f0232
authored
3月 07, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
A Java parser / Java to C converter.
上级
460425a5
全部展开
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
1724 行增加
和
0 行删除
+1724
-0
ClassObj.java
h2/src/tools/org/h2/java/ClassObj.java
+208
-0
Expr.java
h2/src/tools/org/h2/java/Expr.java
+167
-0
JavaParser.java
h2/src/tools/org/h2/java/JavaParser.java
+1025
-0
Statement.java
h2/src/tools/org/h2/java/Statement.java
+202
-0
Test.java
h2/src/tools/org/h2/java/Test.java
+68
-0
TestApp.java
h2/src/tools/org/h2/java/TestApp.java
+54
-0
没有找到文件。
h2/src/tools/org/h2/java/ClassObj.java
0 → 100644
浏览文件 @
a14f0232
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
java
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
/**
* A class or interface.
*/
public
class
ClassObj
{
/**
* The fully qualified class name.
*/
String
name
;
/**
* Whether this is an interface.
*/
boolean
isInterface
;
/**
* Whether this class is public.
*/
boolean
isPublic
;
/**
* Whether this is a primitive class (int, char,...)
*/
boolean
isPrimitive
;
/**
* The imported classes.
*/
ArrayList
<
ClassObj
>
imports
=
new
ArrayList
<
ClassObj
>();
/**
* The per-instance fields.
*/
LinkedHashMap
<
String
,
FieldObj
>
instanceFields
=
new
LinkedHashMap
<
String
,
FieldObj
>();
/**
* The static fields of this class.
*/
LinkedHashMap
<
String
,
FieldObj
>
staticFields
=
new
LinkedHashMap
<
String
,
FieldObj
>();
/**
* The methods.
*/
LinkedHashMap
<
String
,
MethodObj
>
methods
=
new
LinkedHashMap
<
String
,
MethodObj
>();
/**
* Add a method.
*
* @param method the method
*/
void
addMethod
(
MethodObj
method
)
{
methods
.
put
(
method
.
name
,
method
);
}
/**
* Add an instance field.
*
* @param field the field
*/
void
addInstanceField
(
FieldObj
field
)
{
instanceFields
.
put
(
field
.
name
,
field
);
}
/**
* Add a static field.
*
* @param field the field
*/
void
addStaticField
(
FieldObj
field
)
{
staticFields
.
put
(
field
.
name
,
field
);
}
public
String
toString
()
{
if
(
isPrimitive
)
{
return
name
;
}
return
"struct "
+
name
;
}
}
/**
* A method.
*/
class
MethodObj
{
/**
* Whether this method is static.
*/
boolean
isStatic
;
/**
* Whether this method is private.
*/
boolean
isPrivate
;
/**
* The name.
*/
String
name
;
/**
* The statement block (if any).
*/
Statement
block
;
/**
* The return type.
*/
Type
returnType
;
/**
* The parameter list.
*/
ArrayList
<
FieldObj
>
parameters
=
new
ArrayList
<
FieldObj
>();
/**
* Whether this method is final.
*/
boolean
isFinal
;
/**
* Whether this method is public.
*/
boolean
isPublic
;
}
/**
* A field.
*/
class
FieldObj
{
/**
* The type.
*/
Type
type
;
/**
* The field name.
*/
String
name
;
/**
* Whether this field is static.
*/
boolean
isStatic
;
/**
* Whether this field is final.
*/
boolean
isFinal
;
/**
* Whether this field is private.
*/
boolean
isPrivate
;
/**
* Whether this field is public.
*/
boolean
isPublic
;
/**
* The initial value expression (may be null).
*/
Expr
value
;
}
/**
* A type.
*/
class
Type
{
/**
* The class.
*/
ClassObj
type
;
/**
* The array nesting level. 0 if not an array.
*/
int
arrayLevel
;
public
String
toString
()
{
if
(
arrayLevel
==
0
)
{
return
type
.
name
;
}
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
JavaParser
.
toC
(
type
.
name
));
for
(
int
i
=
0
;
i
<
arrayLevel
;
i
++)
{
buff
.
append
(
"[]"
);
}
return
buff
.
toString
();
}
}
h2/src/tools/org/h2/java/Expr.java
0 → 100644
浏览文件 @
a14f0232
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
java
;
import
java.util.ArrayList
;
/**
* An expression.
*/
public
interface
Expr
{
// toString
}
/**
* A method call.
*/
class
CallExpr
implements
Expr
{
String
object
;
ArrayList
<
Expr
>
args
=
new
ArrayList
<
Expr
>();
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
JavaParser
.
toC
(
object
)).
append
(
"("
);
int
i
=
0
;
for
(
Expr
a
:
args
)
{
if
(
i
>
0
)
{
buff
.
append
(
", "
);
}
i
++;
buff
.
append
(
a
);
}
return
buff
.
append
(
")"
).
toString
();
}
}
/**
* A assignment expression.
*/
class
AssignExpr
implements
Expr
{
Expr
left
;
String
op
;
Expr
right
;
public
String
toString
()
{
return
left
+
" "
+
op
+
" "
+
right
;
}
}
/**
* A conditional expression.
*/
class
ConditionalExpr
implements
Expr
{
Expr
condition
;
Expr
ifTrue
,
ifFalse
;
public
String
toString
()
{
return
condition
+
" ? "
+
ifTrue
+
" : "
+
ifFalse
;
}
}
/**
* A literal.
*/
class
LiteralExpr
implements
Expr
{
String
literal
;
public
String
toString
()
{
return
literal
;
}
}
/**
* An operation.
*/
class
OpExpr
implements
Expr
{
Expr
left
;
String
op
;
Expr
right
;
public
String
toString
()
{
if
(
left
==
null
)
{
return
op
+
right
;
}
else
if
(
right
==
null
)
{
return
left
+
op
;
}
return
left
+
" "
+
op
+
" "
+
right
;
}
}
/**
* A "new" expression.
*/
class
NewExpr
implements
Expr
{
String
className
;
public
String
toString
()
{
return
"new "
+
className
;
}
}
/**
* A String literal.
*/
class
StringExpr
implements
Expr
{
/**
* The literal.
*/
String
text
;
public
String
toString
()
{
return
"\""
+
javaEncode
(
text
)
+
"\""
;
}
/**
* Encode the String to Java syntax.
*
* @param s the string
* @return the encoded string
*/
static
String
javaEncode
(
String
s
)
{
StringBuilder
buff
=
new
StringBuilder
(
s
.
length
());
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++)
{
char
c
=
s
.
charAt
(
i
);
switch
(
c
)
{
case
'\t'
:
// HT horizontal tab
buff
.
append
(
"\\t"
);
break
;
case
'\n'
:
// LF linefeed
buff
.
append
(
"\\n"
);
break
;
case
'\f'
:
// FF form feed
buff
.
append
(
"\\f"
);
break
;
case
'\r'
:
// CR carriage return
buff
.
append
(
"\\r"
);
break
;
case
'"'
:
// double quote
buff
.
append
(
"\\\""
);
break
;
case
'\\'
:
// backslash
buff
.
append
(
"\\\\"
);
break
;
default
:
int
ch
=
c
&
0xffff
;
if
(
ch
>=
' '
&&
(
ch
<
0x80
))
{
buff
.
append
(
c
);
// not supported in properties files
// } else if(ch < 0xff) {
// buff.append("\\");
// // make sure it's three characters (0x200 is octal 1000)
// buff.append(Integer.toOctalString(0x200 | ch).substring(1));
}
else
{
buff
.
append
(
"\\u"
);
// make sure it's four characters
buff
.
append
(
Integer
.
toHexString
(
0x10000
|
ch
).
substring
(
1
));
}
}
}
return
buff
.
toString
();
}
}
h2/src/tools/org/h2/java/JavaParser.java
0 → 100644
浏览文件 @
a14f0232
差异被折叠。
点击展开。
h2/src/tools/org/h2/java/Statement.java
0 → 100644
浏览文件 @
a14f0232
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
java
;
import
java.util.ArrayList
;
/**
* A statement.
*/
public
interface
Statement
{
// toString
}
/**
* A "return" statement.
*/
class
ReturnStatement
implements
Statement
{
Expr
expr
;
public
String
toString
()
{
return
"return "
+
(
expr
==
null
?
""
:
expr
)
+
";"
;
}
}
/**
* A "do .. while" statement.
*/
class
DoWhileStatement
implements
Statement
{
Expr
condition
;
Statement
block
;
public
String
toString
()
{
return
"do {\n"
+
block
+
"} while ("
+
condition
+
");"
;
}
}
/**
* A "continue" statement.
*/
class
ContinueStatement
implements
Statement
{
public
String
toString
()
{
return
"continue;"
;
}
}
/**
* A "break" statement.
*/
class
BreakStatement
implements
Statement
{
public
String
toString
()
{
return
"break;"
;
}
}
/**
* An empty statement.
*/
class
EmptyStatement
implements
Statement
{
public
String
toString
()
{
return
";"
;
}
}
/**
* A "switch" statement.
*/
class
SwitchStatement
implements
Statement
{
Expr
expr
;
StatementBlock
defaultBlock
;
ArrayList
<
Expr
>
cases
=
new
ArrayList
<
Expr
>();
ArrayList
<
StatementBlock
>
blocks
=
new
ArrayList
<
StatementBlock
>();
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
"switch ("
).
append
(
expr
).
append
(
") {\n"
);
for
(
int
i
=
0
;
i
<
cases
.
size
();
i
++)
{
buff
.
append
(
"case: "
+
cases
.
get
(
i
)
+
":\n"
);
buff
.
append
(
blocks
.
get
(
i
).
toString
());
}
if
(
defaultBlock
!=
null
)
{
buff
.
append
(
"default:\n"
);
buff
.
append
(
defaultBlock
.
toString
());
}
buff
.
append
(
"}"
);
return
buff
.
toString
();
}
}
/**
* An expression statement.
*/
class
ExprStatement
implements
Statement
{
Expr
expr
;
public
String
toString
()
{
return
expr
+
";"
;
}
}
/**
* A "while" statement.
*/
class
WhileStatement
implements
Statement
{
Expr
condition
;
Statement
block
;
public
String
toString
()
{
String
w
=
"while ("
+
condition
+
")"
;
String
s
=
block
.
toString
();
return
w
+
"\n"
+
s
;
}
}
/**
* An "if" statement.
*/
class
IfStatement
implements
Statement
{
Expr
condition
;
Statement
block
;
Statement
elseBlock
;
public
String
toString
()
{
String
w
=
"if ("
+
condition
+
") {\n"
;
String
s
=
block
.
toString
();
if
(
elseBlock
!=
null
)
{
s
+=
"} else {\n"
+
elseBlock
.
toString
();
}
return
w
+
s
+
"}"
;
}
}
/**
* A "for" statement.
*/
class
ForStatement
implements
Statement
{
Statement
init
;
Expr
condition
;
Expr
update
;
Statement
block
;
ArrayList
<
Expr
>
updates
=
new
ArrayList
<
Expr
>();
public
String
toString
()
{
StringBuffer
buff
=
new
StringBuffer
();
buff
.
append
(
"for ("
).
append
(
init
.
toString
());
buff
.
append
(
" "
).
append
(
condition
.
toString
()).
append
(
"; "
);
for
(
int
i
=
0
;
i
<
updates
.
size
();
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
", "
);
}
buff
.
append
(
updates
.
get
(
i
));
}
buff
.
append
(
") {\n"
);
buff
.
append
(
block
.
toString
()).
append
(
"}"
);
return
buff
.
toString
();
}
}
/**
* A statement block.
*/
class
StatementBlock
implements
Statement
{
ArrayList
<
Statement
>
instructions
=
new
ArrayList
<
Statement
>();
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
for
(
Statement
s
:
instructions
)
{
buff
.
append
(
JavaParser
.
indent
(
s
.
toString
()));
}
return
buff
.
toString
();
}
}
/**
* A variable declaration.
*/
class
VarDecStatement
implements
Statement
{
Type
type
;
ArrayList
<
String
>
variables
=
new
ArrayList
<
String
>();
ArrayList
<
Expr
>
values
=
new
ArrayList
<
Expr
>();
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
type
).
append
(
' '
);
for
(
int
i
=
0
;
i
<
variables
.
size
();
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
", "
);
}
buff
.
append
(
variables
.
get
(
i
));
Expr
value
=
values
.
get
(
i
);
if
(
value
!=
null
)
{
buff
.
append
(
" = "
).
append
(
value
);
}
}
buff
.
append
(
";"
);
return
buff
.
toString
();
}
}
/**
* A variable.
*/
class
VariableExpr
implements
Expr
{
public
String
name
;
public
String
toString
()
{
return
name
;
}
}
h2/src/tools/org/h2/java/Test.java
0 → 100644
浏览文件 @
a14f0232
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
java
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
org.h2.test.TestBase
;
/**
* A test for the Java parser.
*/
public
class
Test
extends
TestBase
{
/**
* Start the task with the given arguments.
*
* @param args the arguments, or null
*/
public
static
void
main
(
String
...
args
)
throws
IOException
{
new
Test
().
test
();
}
public
void
test
()
throws
IOException
{
// not supported yet:
// annotations
// HexadecimalFloatingPointLiteral
// import static
// import *
// only public or default level classes
// initializer blocks
// final variables (within blocks, parameter list)
// Identifier : (labels)
// ClassOrInterfaceDeclaration within blocks (or any other nested classes)
// assert
// array declaration at weird places: int x() [] { return null; }
assertEquals
(
"\\\\"
+
"u0000"
,
JavaParser
.
replaceUnicode
(
"\\\\"
+
"u0000"
));
assertEquals
(
"\u0000"
,
JavaParser
.
replaceUnicode
(
"\\"
+
"u0000"
));
assertEquals
(
"\u0000"
,
JavaParser
.
replaceUnicode
(
"\\"
+
"uu0000"
));
assertEquals
(
"\\\\"
+
"\u0000"
,
JavaParser
.
replaceUnicode
(
"\\\\\\"
+
"u0000"
));
assertEquals
(
"0"
,
JavaParser
.
readNumber
(
"0a"
));
assertEquals
(
"0l"
,
JavaParser
.
readNumber
(
"0l"
));
assertEquals
(
"0xFFL"
,
JavaParser
.
readNumber
(
"0xFFLx"
));
assertEquals
(
"0xDadaCafe"
,
JavaParser
.
readNumber
(
"0xDadaCafex"
));
assertEquals
(
"1.40e-45f"
,
JavaParser
.
readNumber
(
"1.40e-45fx"
));
assertEquals
(
"1e1f"
,
JavaParser
.
readNumber
(
"1e1fx"
));
assertEquals
(
"2.f"
,
JavaParser
.
readNumber
(
"2.fx"
));
assertEquals
(
".3d"
,
JavaParser
.
readNumber
(
".3dx"
));
assertEquals
(
"6.022137e+23f"
,
JavaParser
.
readNumber
(
"6.022137e+23f+1"
));
JavaParser
parser
=
new
JavaParser
(
"src/tools"
,
"org.h2.java.TestApp"
);
parser
.
parse
();
PrintWriter
w
=
new
PrintWriter
(
System
.
out
);
parser
.
writeC
(
w
);
w
.
flush
();
w
=
new
PrintWriter
(
new
FileWriter
(
"bin/test.c"
));
parser
.
writeC
(
w
);
w
.
close
();
}
}
h2/src/tools/org/h2/java/TestApp.java
0 → 100644
浏览文件 @
a14f0232
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
java
;
/**
* A test application.
*/
public
class
TestApp
{
private
static
final
int
FINAL_VALUE
=
10
;
public
static
void
main
(
String
...
args
)
{
System
.
out
.
println
(
"Hello World"
);
}
/**
* A test method.
*
* @param name ignored
* @param x ignored
* @return ignored
*/
public
int
getName
(
int
name
,
int
x
)
{
System
.
out
.
println
(
"Hello"
);
int
m
=
x
;
m
=
FINAL_VALUE
;
switch
(
x
)
{
case
1
:
m
=
3
;
m
=
4
;
break
;
default
:
m
=
4
;
m
=
5
;
}
for
(
int
i
=
0
;
i
<
10
;
i
++,
i
--)
{
getName
(
0
,
0
);
}
if
(
m
>
0
)
{
getName
(
2
,
3
);
}
else
{
getName
(
1
,
12
);
}
do
{
getName
(
0
,
0
);
return
name
;
}
while
(
true
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论