Skip to content

Commit c4dd3ab

Browse files
Mike KasianowiczMike Kasianowicz
Mike Kasianowicz
authored and
Mike Kasianowicz
committed
initial commit
1 parent 31a5f5a commit c4dd3ab

28 files changed

+5717
-0
lines changed

Diff for: Argument.cs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Anarres C Preprocessor
3+
* Copyright (c) 2007-2008, Shevek
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Text;
20+
21+
namespace CppNet {
22+
/**
23+
* A macro argument.
24+
*
25+
* This encapsulates a raw and preprocessed token stream.
26+
*/
27+
internal class Argument : List<Token> {
28+
public const int NO_ARGS = -1;
29+
30+
private List<Token> _expansion;
31+
32+
public Argument() {
33+
this._expansion = null;
34+
}
35+
36+
public void addToken(Token tok) {
37+
Add(tok);
38+
}
39+
40+
internal void expand(Preprocessor p) {
41+
/* Cache expansion. */
42+
if(_expansion == null) {
43+
this._expansion = p.expand(this);
44+
// System.out.println("Expanded arg " + this);
45+
}
46+
}
47+
48+
public Iterator<Token> expansion()
49+
{
50+
return _expansion.iterator();
51+
}
52+
53+
override public String ToString() {
54+
StringBuilder buf = new StringBuilder();
55+
buf.Append("Argument(");
56+
// buf.Append(super.toString());
57+
buf.Append("raw=[ ");
58+
for (int i = 0; i < this.Count; i++)
59+
buf.Append(this[i].getText());
60+
buf.Append(" ];expansion=[ ");
61+
if(_expansion == null)
62+
buf.Append("null");
63+
else
64+
for(int i = 0; i < _expansion.Count; i++)
65+
buf.Append(_expansion[i].getText());
66+
buf.Append(" ])");
67+
return buf.ToString();
68+
}
69+
70+
}
71+
72+
}

Diff for: CppNet.csproj

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{C2FD9262-69F8-4B75-9AB1-FF359C9143E9}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>CppNet</RootNamespace>
11+
<AssemblyName>CppNet</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Xml" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="Argument.cs" />
43+
<Compile Include="Feature.cs" />
44+
<Compile Include="FileLexerSource.cs" />
45+
<Compile Include="FixedTokenSource.cs" />
46+
<Compile Include="JavaCompat\JavaCompat.cs" />
47+
<Compile Include="JavaFile.cs" />
48+
<Compile Include="JavaFileSystem.cs" />
49+
<Compile Include="JoinReader.cs" />
50+
<Compile Include="LexerException.cs" />
51+
<Compile Include="LexerSource.cs" />
52+
<Compile Include="Macro.cs" />
53+
<Compile Include="MacroTokenSource.cs" />
54+
<Compile Include="Preprocessor.cs" />
55+
<Compile Include="PreprocessorListener.cs" />
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="Source.cs" />
58+
<Compile Include="SourceIterator.cs" />
59+
<Compile Include="State.cs" />
60+
<Compile Include="StringLexerSource.cs" />
61+
<Compile Include="Token.cs" />
62+
<Compile Include="VirtualFile.cs" />
63+
<Compile Include="VirtualFileSystem.cs" />
64+
<Compile Include="Warning.cs" />
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>

Diff for: CppReader.cs

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Anarres C Preprocessor
3+
* Copyright (c) 2007-2008, Shevek
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package org.anarres.cpp;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.Reader;
23+
24+
import static org.anarres.cpp.Token.*;
25+
26+
/**
27+
* A Reader wrapper around the Preprocessor.
28+
*
29+
* This is a utility class to provide a transparent {@link Reader}
30+
* which preprocesses the input text.
31+
*
32+
* @see Preprocessor
33+
* @see Reader
34+
*/
35+
public class CppReader extends Reader {
36+
37+
private Preprocessor cpp;
38+
private String token;
39+
private int idx;
40+
41+
public CppReader(final Reader r) {
42+
cpp = new Preprocessor(new LexerSource(r, true) {
43+
@Override
44+
public String getName() {
45+
return "<CppReader Input@" +
46+
System.identityHashCode(r) + ">";
47+
}
48+
});
49+
token = "";
50+
idx = 0;
51+
}
52+
53+
public CppReader(Preprocessor p) {
54+
cpp = p;
55+
token = "";
56+
idx = 0;
57+
}
58+
59+
/**
60+
* Returns the Preprocessor used by this CppReader.
61+
*/
62+
public Preprocessor getPreprocessor() {
63+
return cpp;
64+
}
65+
66+
/**
67+
* Defines the given name as a macro.
68+
*
69+
* This is a convnience method.
70+
*/
71+
public void addMacro(String name)
72+
throws LexerException {
73+
cpp.addMacro(name);
74+
}
75+
76+
/**
77+
* Defines the given name as a macro.
78+
*
79+
* This is a convnience method.
80+
*/
81+
public void addMacro(String name, String value)
82+
throws LexerException {
83+
cpp.addMacro(name, value);
84+
}
85+
86+
private boolean refill()
87+
throws IOException {
88+
try {
89+
assert cpp != null : "cpp is null : was it closed?";
90+
if (token == null)
91+
return false;
92+
while (idx >= token.length()) {
93+
Token tok = cpp.token();
94+
switch (tok.getType()) {
95+
case EOF:
96+
token = null;
97+
return false;
98+
case CCOMMENT:
99+
case CPPCOMMENT:
100+
if (!cpp.getFeature(Feature.KEEPCOMMENTS)) {
101+
token = " ";
102+
break;
103+
}
104+
default:
105+
token = tok.getText();
106+
break;
107+
}
108+
idx = 0;
109+
}
110+
return true;
111+
}
112+
catch (LexerException e) {
113+
/* Never happens.
114+
if (e.getCause() instanceof IOException)
115+
throw (IOException)e.getCause();
116+
*/
117+
IOException ie = new IOException(String.valueOf(e));
118+
ie.initCause(e);
119+
throw ie;
120+
}
121+
}
122+
123+
public int read()
124+
throws IOException {
125+
if (!refill())
126+
return -1;
127+
return token.charAt(idx++);
128+
}
129+
130+
/* XXX Very slow and inefficient. */
131+
public int read(char cbuf[], int off, int len)
132+
throws IOException {
133+
if (token == null)
134+
return -1;
135+
for (int i = 0; i < len; i++) {
136+
int ch = read();
137+
if (ch == -1)
138+
return i;
139+
cbuf[off + i] = (char)ch;
140+
}
141+
return len;
142+
}
143+
144+
public void close()
145+
throws IOException {
146+
if (cpp != null) {
147+
cpp.close();
148+
cpp = null;
149+
}
150+
token = null;
151+
}
152+
153+
}

0 commit comments

Comments
 (0)