Commit e878933e authored by joni's avatar joni
Browse files

Add project files.

parent 025cb12d
Pipeline #44 canceled with stages

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28922.388
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ParserExercise", "ParserExercise\ParserExercise.vcxproj", "{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Debug|x64.ActiveCfg = Debug|x64
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Debug|x64.Build.0 = Debug|x64
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Debug|x86.ActiveCfg = Debug|Win32
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Debug|x86.Build.0 = Debug|Win32
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Release|x64.ActiveCfg = Release|x64
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Release|x64.Build.0 = Release|x64
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Release|x86.ActiveCfg = Release|Win32
{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {899D985A-271D-4306-92EC-8B62FB83B1E8}
EndGlobalSection
EndGlobal
#include "Access.h"
#include "Sentence.h"
#include "Scope.h"
Access::Access(void* scope, int line,std::string var) : Sentence(scope,line) {
_var = var;
}
std::string Access::Var() {
return _var;
}
bool Access::Eval() {
return SCOPE->EvalAccess(this);
//return std::string();
}
#pragma once
#include "Sentence.h"
class Access : public Sentence {
private:
std::string _var;
public:
Access(void* scope,int line,std::string var);
virtual /*std::string*/ bool Eval();
std::string Var();
};
\ No newline at end of file
#include "Declare.h"
#include "Sentence.h"
#include "Scope.h"
Declare::Declare(void* scope, int line,std::string var) : Sentence(scope, line) {
_var = var;
}
std::string Declare::Var() {
return _var;
}
/*std::string*/ bool Declare::Eval() {
return SCOPE->EvalDeclare(this);
}
#pragma once
#include "Sentence.h"
class Declare : public Sentence {
private:
std::string _var;
public:
Declare(void* scope,int line,std::string var);
virtual /*std::string*/ bool Eval();
std::string Var();
};
\ No newline at end of file
#include "Parser.h"
#include "Scope.h"
#include "Access.h"
#include "Declare.h"
#include <algorithm>
std::string ltrim(const std::string& s)
{
size_t start = s.find_first_not_of(" \n\r\t\f\v");
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string& s)
{
size_t end = s.find_last_not_of(" \n\r\t\f\v");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string& s)
{
return rtrim(ltrim(s));
}
SentenceParser::SentenceParser(Scope* scope) {
_scope = scope;
}
void SentenceParser::Load(std::string data) {
_activeData = trim(data);
//_dataList.push_back(data);
}
Sentence* SentenceParser::createScopeSentence(std::string sentenceData, int line) {
const char* body = strstr(sentenceData.c_str(), "{");
return new Scope(_scope,line,trim(std::string(sentenceData.c_str() + 5 ,body-sentenceData.c_str() - 5)),trim(std::string(body)));
}
Sentence* SentenceParser::createSentence(std::string sentenceData, int line) {
const char* body = strstr(sentenceData.c_str(), " ");
if (strstr(sentenceData.c_str(), "DECLARE") == sentenceData.c_str()) {
return new Declare(_scope,line,trim(std::string(body,strlen(body) -1)));
}
else if (strstr(sentenceData.c_str(), "ACCESS") == sentenceData.c_str()) {
return new Access(_scope,line,trim(std::string(body, strlen(body) - 1)));
}
return NULL;
}
int find_count(std::string str,int spos, int epos, const char* c) {
size_t pos = spos;
size_t tPos = spos;
int count = 0;
while (((pos = str.find_first_of(c,pos)) != str.npos)) {
if (pos == tPos) {
pos++;
tPos = pos;
continue;
}else {
tPos = pos;
}
if (pos >= epos) {
break;
}
count++;
}
return count+1;
}
Sentence* SentenceParser::Next() {
if(_activePosition == _activeData.size() && _dataList.empty()) {
return NULL;
}
size_t n_sentence_pos;
size_t scope_begin = _activeData.find_first_of('{', _activePosition);
size_t other_end = _activeData.find_first_of(';',_activePosition);
if (scope_begin == _activeData.npos && other_end == _activeData.npos) {
return NULL;
}
Sentence* sentence;
if (scope_begin != _activeData.npos && scope_begin < other_end) {
size_t scope_end = _activeData.find_last_of('}');
n_sentence_pos = scope_end;
sentence = createScopeSentence(trim(std::string(_activeData.c_str() + _activePosition, n_sentence_pos - _activePosition + 1)),find_count(_activeData,0,scope_begin,"\n"));
}else {
n_sentence_pos = other_end;
sentence = createSentence(trim(std::string(_activeData.c_str() + _activePosition, n_sentence_pos - _activePosition + 1)),find_count(_activeData,0,other_end,"\n"));
}
_activePosition = n_sentence_pos+1;
return sentence;
}
\ No newline at end of file
#pragma once
#include <string>
#include <vector>
#include "Sentence.h"
#include "Scope.h"
class SentenceParser {
private:
std::vector<std::string> _dataList;
std::string _activeData;
char* _activeBuffer;
int _activePosition = 0;
Scope* _scope;
Sentence* createScopeSentence(std::string sentenceData, int line);
Sentence* createSentence(std::string sentenceData, int line);
public:
void Load(std::string data);
Sentence* Next();
SentenceParser(Scope* scope);
};
\ No newline at end of file
// ParserExercise.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include "Parser.h"
std::string readFile(const char* file) {
std::ifstream inFile;
inFile.open(file);
std::stringstream strStream;
strStream << inFile.rdbuf();
inFile.close();
return strStream.str();
}
int main(int argc, char* argv[])
{
try {
SentenceParser mainParser = SentenceParser(NULL);
mainParser.Load(readFile("simple.txt"));
bool error = false;
for (Sentence* sentence = mainParser.Next(); sentence != NULL; sentence = mainParser.Next()) {
if (!sentence->Eval()) {
error = true;
}
//delete sentence;
}
if (!error) {
printf("SUCCESS\n");
}
}
catch(_exception e){
printf("SYNTAX ERROR\n");
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{C40DFD2A-C694-4023-BA0E-9EB06DEDD4F4}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>ParserExercise</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Access.cpp" />
<ClCompile Include="Declare.cpp" />
<ClCompile Include="error.cpp" />
<ClCompile Include="ParserExercise.cpp" />
<ClCompile Include="Parser.cpp" />
<ClCompile Include="Scope.cpp" />
<ClCompile Include="Sentence.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Access.h" />
<ClInclude Include="Declare.h" />
<ClInclude Include="error.h" />
<ClInclude Include="Parser.h" />
<ClInclude Include="Scope.h" />
<ClInclude Include="Sentence.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Source Files\Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ParserExercise.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Parser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Scope.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Access.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Declare.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Sentence.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="error.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Sentence.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Scope.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Access.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Declare.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Parser.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="error.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#include "Scope.h"
#include "Parser.h"
#include "Sentence.h"
#include "error.h"
static Scope _global = Scope();
Scope* Scope::Global() {
return &_global;
}
Scope::Scope() : Sentence(this,0) {
_name = std::string();
}
Scope::Scope(Scope* scope , int line,std::string name, std::string body) : Sentence(scope,line) {
_parserPtr = new SentenceParser(this);
_name = name;
if (body != "") {
PARSER->Load(body.substr(1, body.length() - 2));
}
if (scope == NULL) {
scope = Global();
}
scope->RegScope(this);
}
/*std::string*/ bool Scope::Eval() {
std::string val;
bool allSuccess = true;
for (Sentence* sentence = PARSER->Next();sentence != NULL; sentence = PARSER->Next()) {
if (!sentence->Eval()) {
allSuccess = false;
}
}
return allSuccess;
}
bool Scope::FindVar(std::string var, bool recursive) {
for (int i = 0; i < _declares.size(); i++) {
if (_declares[i]->Var() == var) {
return true;
}
}
if (recursive) {
for (int i = 0; i < _scopes.size(); i++) {
if (_scopes[i]->FindVar(var,recursive)) {
return true;
}
}
}
return false;
}
Scope* Scope::FindScope(std::string s, bool recursive) {
if (!recursive) {
for (int i = 0; i < _scopes.size(); i++) {
if (_scopes[i]->Name() == s) {
return _scopes[i];
}
}
return NULL;
}else {
for (int i = 0; i < _scopes.size(); i++) {
if (_scopes[i]->Name() == s) {
return _scopes[i];
}else {
Scope* sc = _scopes[i]->FindScope(s,recursive);
if (sc != Global()) {
return sc;
}
}
}
return NULL;;
}
}
std::string Scope::Name() {
return _name;
}
void Scope::RegScope(Scope* scope) {
scope->_scopes.push_back(this);
//_scopes.push_back(scope);
}
bool Scope::EvalDeclare(Declare* var) {
if (!FindVar(var->Var(),true) && FindScope(var->Var(),true) == NULL/* && var.Var().find("::") == std::string::npos*/) {
_declares.push_back(var);
}else {
print_error(var->Line());
return false;
}
return true;
}