Skip to content

Commit 25cd339

Browse files
author
Satya
committed
First version that can do significant amount of C/C++ parsing.
1 parent bf3ec73 commit 25cd339

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+30850
-3
lines changed

Diff for: README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
cppparser
2-
=========
1+
# CppParser #
32

4-
A library to parse C/C++ source as DOM
3+
A library to parse C/C++ source.
4+
It does not do pre-processing and also tries not to ignore comments.
5+
The result of parsing is the DOM structure where each elements of a file are arranged in a DOM tree.
6+
The parsing is extremely lenient and it is expected that only valid C/C++ file is given as input to CppParser.
7+
8+
## Motivation ##
9+
CppParser can be used to build tools that need parsing of C/C++ files.

Diff for: cppparser.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include "cppdom.h"
25+
26+
//////////////////////////////////////////////////////////////////////////
27+
28+
extern CppCompound* parseFile(FILE* fp);
29+
30+
CppCompound* parseSingleFile(const char* filename)
31+
{
32+
FILE* fp = fopen(filename, "r");
33+
if(fp == NULL)
34+
return NULL;
35+
CppCompound* cppCompound = parseFile(fp);
36+
if(cppCompound == NULL)
37+
return cppCompound;
38+
cppCompound->name_ = filename;
39+
return cppCompound;
40+
}

Diff for: cppparser.sln

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppparser", "cppparser.vcxproj", "{345C65D4-A3E2-43AB-AE1C-BA295119C74E}"
5+
ProjectSection(ProjectDependencies) = postProject
6+
{F5518709-54D4-42A7-A64B-51C316CEDFA4} = {F5518709-54D4-42A7-A64B-51C316CEDFA4}
7+
EndProjectSection
8+
EndProject
9+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "btyacc", "third_party\btyacc_tp\btyacc.vcxproj", "{F5518709-54D4-42A7-A64B-51C316CEDFA4}"
10+
EndProject
11+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppparsertest", "cppparsertest\cppparsertest.vcxproj", "{C16C48D9-46C8-4D04-AF9E-FE272E31DAAB}"
12+
ProjectSection(ProjectDependencies) = postProject
13+
{345C65D4-A3E2-43AB-AE1C-BA295119C74E} = {345C65D4-A3E2-43AB-AE1C-BA295119C74E}
14+
EndProjectSection
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Win32 = Debug|Win32
19+
Release|Win32 = Release|Win32
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{345C65D4-A3E2-43AB-AE1C-BA295119C74E}.Debug|Win32.ActiveCfg = Debug|Win32
23+
{345C65D4-A3E2-43AB-AE1C-BA295119C74E}.Debug|Win32.Build.0 = Debug|Win32
24+
{345C65D4-A3E2-43AB-AE1C-BA295119C74E}.Release|Win32.ActiveCfg = Release|Win32
25+
{345C65D4-A3E2-43AB-AE1C-BA295119C74E}.Release|Win32.Build.0 = Release|Win32
26+
{F5518709-54D4-42A7-A64B-51C316CEDFA4}.Debug|Win32.ActiveCfg = Debug|Win32
27+
{F5518709-54D4-42A7-A64B-51C316CEDFA4}.Debug|Win32.Build.0 = Debug|Win32
28+
{F5518709-54D4-42A7-A64B-51C316CEDFA4}.Release|Win32.ActiveCfg = Release|Win32
29+
{F5518709-54D4-42A7-A64B-51C316CEDFA4}.Release|Win32.Build.0 = Release|Win32
30+
{C16C48D9-46C8-4D04-AF9E-FE272E31DAAB}.Debug|Win32.ActiveCfg = Debug|Win32
31+
{C16C48D9-46C8-4D04-AF9E-FE272E31DAAB}.Debug|Win32.Build.0 = Debug|Win32
32+
{C16C48D9-46C8-4D04-AF9E-FE272E31DAAB}.Release|Win32.ActiveCfg = Release|Win32
33+
{C16C48D9-46C8-4D04-AF9E-FE272E31DAAB}.Release|Win32.Build.0 = Release|Win32
34+
EndGlobalSection
35+
GlobalSection(SolutionProperties) = preSolution
36+
HideSolutionNode = FALSE
37+
EndGlobalSection
38+
EndGlobal

Diff for: cppparser.vcxproj

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{345C65D4-A3E2-43AB-AE1C-BA295119C74E}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>cppparser</RootNamespace>
17+
</PropertyGroup>
18+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
20+
<ConfigurationType>StaticLibrary</ConfigurationType>
21+
<UseDebugLibraries>true</UseDebugLibraries>
22+
<CharacterSet>Unicode</CharacterSet>
23+
</PropertyGroup>
24+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
25+
<ConfigurationType>StaticLibrary</ConfigurationType>
26+
<UseDebugLibraries>false</UseDebugLibraries>
27+
<WholeProgramOptimization>true</WholeProgramOptimization>
28+
<CharacterSet>Unicode</CharacterSet>
29+
</PropertyGroup>
30+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
31+
<ImportGroup Label="ExtensionSettings">
32+
</ImportGroup>
33+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
34+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
35+
</ImportGroup>
36+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
37+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
38+
</ImportGroup>
39+
<PropertyGroup Label="UserMacros" />
40+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
41+
<LinkIncremental>true</LinkIncremental>
42+
<TargetExt>.lib</TargetExt>
43+
<OutDir>..\build_results\$(Configuration)\bin\</OutDir>
44+
<IntDir>..\build_results\$(Configuration)\obj\$(ProjectName)\</IntDir>
45+
</PropertyGroup>
46+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
47+
<LinkIncremental>false</LinkIncremental>
48+
<OutDir>..\build_results\$(Configuration)\bin\</OutDir>
49+
<IntDir>..\build_results\$(Configuration)\obj\$(ProjectName)\</IntDir>
50+
</PropertyGroup>
51+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
52+
<ClCompile>
53+
<PrecompiledHeader>
54+
</PrecompiledHeader>
55+
<WarningLevel>Level3</WarningLevel>
56+
<Optimization>Disabled</Optimization>
57+
<PreprocessorDefinitions>BOOST_AUTO_LINK_NOMANGLE;CPPPARSEREXPORT;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
58+
<AdditionalIncludeDirectories>./hack;./pub;../common/third_party/boost_tp</AdditionalIncludeDirectories>
59+
<DisableSpecificWarnings>4251</DisableSpecificWarnings>
60+
<TreatWarningAsError>true</TreatWarningAsError>
61+
</ClCompile>
62+
<Link>
63+
<SubSystem>Console</SubSystem>
64+
<GenerateDebugInformation>true</GenerateDebugInformation>
65+
</Link>
66+
</ItemDefinitionGroup>
67+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
68+
<ClCompile>
69+
<WarningLevel>Level3</WarningLevel>
70+
<PrecompiledHeader>
71+
</PrecompiledHeader>
72+
<Optimization>MaxSpeed</Optimization>
73+
<FunctionLevelLinking>true</FunctionLevelLinking>
74+
<IntrinsicFunctions>true</IntrinsicFunctions>
75+
<PreprocessorDefinitions>BOOST_AUTO_LINK_NOMANGLE;CPPPARSEREXPORT;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
76+
<AdditionalIncludeDirectories>./hack;./pub;../common/third_party/boost_tp</AdditionalIncludeDirectories>
77+
<DisableSpecificWarnings>4251</DisableSpecificWarnings>
78+
<TreatWarningAsError>true</TreatWarningAsError>
79+
</ClCompile>
80+
<Link>
81+
<SubSystem>Console</SubSystem>
82+
<GenerateDebugInformation>true</GenerateDebugInformation>
83+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
84+
<OptimizeReferences>true</OptimizeReferences>
85+
</Link>
86+
</ItemDefinitionGroup>
87+
<ItemGroup>
88+
<ClCompile Include="cppparser.cpp">
89+
<PreprocessToFile Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</PreprocessToFile>
90+
<PreprocessToFile Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</PreprocessToFile>
91+
</ClCompile>
92+
<ClCompile Include="cppprog.cpp" />
93+
<ClCompile Include="cppwriter.cpp" />
94+
<ClCompile Include="parser.lex.cpp" />
95+
<ClCompile Include="parser.tab.cpp" />
96+
</ItemGroup>
97+
<ItemGroup>
98+
<CustomBuild Include="parser.l">
99+
<FileType>Document</FileType>
100+
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flex -oparser.lex.cpp parser.l
101+
</Command>
102+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">parser.lex.cpp</Outputs>
103+
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flex -oparser.lex.cpp parser.l
104+
</Command>
105+
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Running Fast lexer for parser.l</Message>
106+
<Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Running Fast lexer for parser.l</Message>
107+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">parser.lex.cpp</Outputs>
108+
</CustomBuild>
109+
<CustomBuild Include="parser.y">
110+
<FileType>Document</FileType>
111+
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\build_results\$(Configuration)\tools\btyacc.exe -d parser.y
112+
XCOPY /D /Y y_tab.c parser.tab.cpp
113+
XCOPY /D /Y y_tab.h parser.tab.h
114+
</Command>
115+
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Running BtYacc</Message>
116+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">y_tab.c;y_tab.h</Outputs>
117+
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">..\build_results\$(Configuration)\tools\btyacc.exe -dl parser.y
118+
XCOPY /D /Y y_tab.c parser.tab.cpp
119+
XCOPY /D /Y y_tab.h parser.tab.h
120+
</Command>
121+
<Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Running BtYacc</Message>
122+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">y_tab.c;y_tab.h</Outputs>
123+
</CustomBuild>
124+
</ItemGroup>
125+
<ItemGroup>
126+
<ClInclude Include="pub\const.h" />
127+
<ClInclude Include="pub\cppdom.h" />
128+
<ClInclude Include="pub\cppparser.h" />
129+
<ClInclude Include="pub\cppprog.h" />
130+
<ClInclude Include="pub\cppwriter.h" />
131+
<ClInclude Include="cpptoken.h" />
132+
<ClInclude Include="parser.tab.h" />
133+
</ItemGroup>
134+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
135+
<ImportGroup Label="ExtensionTargets">
136+
</ImportGroup>
137+
</Project>

Diff for: cppparser.vcxproj.filters

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="cppparser.cpp">
5+
<Filter>src</Filter>
6+
</ClCompile>
7+
<ClCompile Include="parser.lex.cpp">
8+
<Filter>src</Filter>
9+
</ClCompile>
10+
<ClCompile Include="parser.tab.cpp">
11+
<Filter>src</Filter>
12+
</ClCompile>
13+
<ClCompile Include="cppwriter.cpp">
14+
<Filter>src</Filter>
15+
</ClCompile>
16+
<ClCompile Include="cppprog.cpp">
17+
<Filter>src</Filter>
18+
</ClCompile>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<CustomBuild Include="parser.y">
22+
<Filter>src</Filter>
23+
</CustomBuild>
24+
<CustomBuild Include="parser.l">
25+
<Filter>src</Filter>
26+
</CustomBuild>
27+
</ItemGroup>
28+
<ItemGroup>
29+
<ClInclude Include="pub\const.h">
30+
<Filter>pub</Filter>
31+
</ClInclude>
32+
<ClInclude Include="pub\cppdom.h">
33+
<Filter>pub</Filter>
34+
</ClInclude>
35+
<ClInclude Include="cpptoken.h">
36+
<Filter>src</Filter>
37+
</ClInclude>
38+
<ClInclude Include="parser.tab.h">
39+
<Filter>src</Filter>
40+
</ClInclude>
41+
<ClInclude Include="pub\cppparser.h">
42+
<Filter>pub</Filter>
43+
</ClInclude>
44+
<ClInclude Include="pub\cppwriter.h">
45+
<Filter>pub</Filter>
46+
</ClInclude>
47+
<ClInclude Include="pub\cppprog.h">
48+
<Filter>pub</Filter>
49+
</ClInclude>
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Filter Include="pub">
53+
<UniqueIdentifier>{15d15d87-a120-4954-a143-5a85cc737914}</UniqueIdentifier>
54+
</Filter>
55+
<Filter Include="src">
56+
<UniqueIdentifier>{80a98289-e59d-40d8-a2dd-e249d7ced27f}</UniqueIdentifier>
57+
</Filter>
58+
</ItemGroup>
59+
</Project>

Diff for: cppparsertest/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_output/

0 commit comments

Comments
 (0)