From 84c5bceb818127fd7728655fb209a1be92e53fde Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 5 Nov 2013 22:15:19 -0800 Subject: [PATCH] Initial commit --- .gitignore | 3 + .gitmodules | 3 + .vimrc | 10 + Makefile | 55 +++ TreeSitter.xcodeproj/project.pbxproj | 556 +++++++++++++++++++++++++++ spec/externals/igloo | 1 + spec/main.cpp | 8 + spec/rules_spec.cpp | 53 +++ spec_helper.h | 22 ++ src/rules/rule.cpp | 95 +++++ src/rules/rule.h | 68 ++++ src/rules/transition_map.h | 94 +++++ 12 files changed, 968 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .vimrc create mode 100644 Makefile create mode 100644 TreeSitter.xcodeproj/project.pbxproj create mode 160000 spec/externals/igloo create mode 100644 spec/main.cpp create mode 100644 spec/rules_spec.cpp create mode 100644 spec_helper.h create mode 100644 src/rules/rule.cpp create mode 100644 src/rules/rule.h create mode 100644 src/rules/transition_map.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..e96f5dd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +spec/run.out* +TreeSitter.xcodeproj/project.xcworkspace +TreeSitter.xcodeproj/xcuserdata diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..a72846c0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "spec/externals/igloo"] + path = spec/externals/igloo + url = https://github.com/joakimkarlsson/igloo.git diff --git a/.vimrc b/.vimrc new file mode 100644 index 00000000..ea8a0703 --- /dev/null +++ b/.vimrc @@ -0,0 +1,10 @@ +set wildignore+=spec/externals +set wildignore+=TreeSitter.xcodeproj/* +set shiftwidth=4 +set softtabstop=4 +set tabstop=4 + +let g:syntastic_cpp_compiler = 'clang' +let g:syntastic_cpp_auto_refresh_includes = 1 +let g:syntastic_cpp_include_dirs = [ './core/include', './spec/externals/igloo', './spec', './src' ] +let g:syntastic_cpp_compiler_options = '-std=gnu99 -fnested-functions -DRUBY_MISSING_H' diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..5e686623 --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +.PHONY: all clean install install-dev test test-c test-scm valgrind + +### install configuration ### +CPP = c++ +RM ?= rm -f +INSTALL ?= install +MKDIR ?= $(INSTALL) -d +SYMLINK ?= ln -s + +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +LIBDIR ?= $(PREFIX)/lib +SOLIBDIR ?= $(PREFIX)/lib +INCDIR ?= $(PREFIX)/include +MODDIR ?= $(PREFIX)/share +BINMODDIR ?= $(PREFIX)/lib +MANDIR ?= $(PREFIX)/share/man/man1 + +### library configuration ### +LIB_NAME = tree_sitter +DIR = $(shell pwd) +HEADERS = $(wildcard include/*.h) +SOURCES = $(wildcard src/*.cpp) +TESTS = $(wildcard spec/*.cpp) +OBJECTS = $(foreach file, $(C_SOURCES), $(basename $(file)).o) +LIB_FILE = lib$(LIB_NAME)$(SO) +TEST_BIN = spec/run.out + +### build configuration ### +CFLAGS ?= -Wall -std=c++11 -stdlib=libc++ -g -m64 + +### targets ### +all: $(LIB_FILE) + +src/%.o: src/%.cpp + $(CPP) $(CFLAGS) -c $< -o $@ + +test: $(TEST_BIN) + ./$< + +$(TEST_BIN): $(TESTS) $(SOURCES) + $(CPP) $(CFLAGS) -Ispec/externals/igloo -Isrc -L. $^ -o $@ + +# $(TEST_BIN): $(LIB_FILE) $(C_TESTS) + # $(CPP) $(CFLAGS) -Ispec/externals/igloo -Isrc -L. -l$(LIB_NAME) test/c/_runner.cc -o $@ + +debug: $(TEST_BIN) + gdb $< + +valgrind: $(TEST_BIN) + valgrind --track-origins=yes $(TEST_BIN) + +clean: + $(RM) $(OBJECTS) $(LIB_FILE) $(TEST_BIN) + diff --git a/TreeSitter.xcodeproj/project.pbxproj b/TreeSitter.xcodeproj/project.pbxproj new file mode 100644 index 00000000..a88cb261 --- /dev/null +++ b/TreeSitter.xcodeproj/project.pbxproj @@ -0,0 +1,556 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 1214930E181E200B008E9BDA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492E9181E200B008E9BDA /* main.cpp */; }; + 1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492EA181E200B008E9BDA /* rules_spec.cpp */; }; + 12E71853181D081C0051A649 /* rule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E71851181D081C0051A649 /* rule.cpp */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 12E71792181D02A80051A649 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 121306011829FAED00FCF928 /* spec_helper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = spec_helper.h; sourceTree = SOURCE_ROOT; }; + 12149265181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "igloo-tests_CXX_prefix.hxx"; sourceTree = ""; }; + 12149266181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx.gch */ = {isa = PBXFileReference; lastKnownFileType = file; path = "igloo-tests_CXX_prefix.hxx.gch"; sourceTree = ""; }; + 12149267181E200B008E9BDA /* snowhouse-tests_CXX_prefix.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "snowhouse-tests_CXX_prefix.hxx"; sourceTree = ""; }; + 12149268181E200B008E9BDA /* snowhouse-tests_CXX_prefix.hxx.gch */ = {isa = PBXFileReference; lastKnownFileType = file; path = "snowhouse-tests_CXX_prefix.hxx.gch"; sourceTree = ""; }; + 1214926B181E200B008E9BDA /* alternativeregistrationaliases.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = alternativeregistrationaliases.h; sourceTree = ""; }; + 1214926C181E200B008E9BDA /* context.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = context.h; sourceTree = ""; }; + 1214926D181E200B008E9BDA /* contextbase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = contextbase.h; sourceTree = ""; }; + 1214926E181E200B008E9BDA /* contextprovider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = contextprovider.h; sourceTree = ""; }; + 1214926F181E200B008E9BDA /* contextregistry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = contextregistry.h; sourceTree = ""; }; + 12149270181E200B008E9BDA /* contextrunner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = contextrunner.h; sourceTree = ""; }; + 12149271181E200B008E9BDA /* core.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = core.h; sourceTree = ""; }; + 12149272181E200B008E9BDA /* defaultregistrationaliases.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = defaultregistrationaliases.h; sourceTree = ""; }; + 12149274181E200B008E9BDA /* coloredconsoletestresultsoutput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = coloredconsoletestresultsoutput.h; sourceTree = ""; }; + 12149275181E200B008E9BDA /* consoleoutputcolors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = consoleoutputcolors.h; sourceTree = ""; }; + 12149276181E200B008E9BDA /* defaulttestresultsoutput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = defaulttestresultsoutput.h; sourceTree = ""; }; + 12149277181E200B008E9BDA /* outputters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = outputters.h; sourceTree = ""; }; + 12149278181E200B008E9BDA /* testresultsoutput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testresultsoutput.h; sourceTree = ""; }; + 12149279181E200B008E9BDA /* visualstudiooutput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = visualstudiooutput.h; sourceTree = ""; }; + 1214927A181E200B008E9BDA /* xmlwriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xmlwriter.h; sourceTree = ""; }; + 1214927B181E200B008E9BDA /* xunitoutput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xunitoutput.h; sourceTree = ""; }; + 1214927C181E200B008E9BDA /* registration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = registration.h; sourceTree = ""; }; + 1214927D181E200B008E9BDA /* testlistener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testlistener.h; sourceTree = ""; }; + 1214927E181E200B008E9BDA /* testlisteneraggregator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testlisteneraggregator.h; sourceTree = ""; }; + 12149280181E200B008E9BDA /* minimalprogresstestlistener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = minimalprogresstestlistener.h; sourceTree = ""; }; + 12149281181E200B008E9BDA /* testlisteners.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testlisteners.h; sourceTree = ""; }; + 12149282181E200B008E9BDA /* testresult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testresult.h; sourceTree = ""; }; + 12149283181E200B008E9BDA /* testresultfactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testresultfactory.h; sourceTree = ""; }; + 12149284181E200B008E9BDA /* testresults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testresults.h; sourceTree = ""; }; + 12149285181E200B008E9BDA /* testrunner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testrunner.h; sourceTree = ""; }; + 12149288181E200B008E9BDA /* choices.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = choices.h; sourceTree = ""; }; + 1214928B181E200B008E9BDA /* assert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = assert.h; sourceTree = ""; }; + 1214928C181E200B008E9BDA /* assertionexception.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = assertionexception.h; sourceTree = ""; }; + 1214928D181E200B008E9BDA /* assertmacro.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = assertmacro.h; sourceTree = ""; }; + 1214928F181E200B008E9BDA /* constraints.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = constraints.h; sourceTree = ""; }; + 12149290181E200B008E9BDA /* containsconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = containsconstraint.h; sourceTree = ""; }; + 12149291181E200B008E9BDA /* endswithconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = endswithconstraint.h; sourceTree = ""; }; + 12149292181E200B008E9BDA /* equalsconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = equalsconstraint.h; sourceTree = ""; }; + 12149293181E200B008E9BDA /* equalscontainerconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = equalscontainerconstraint.h; sourceTree = ""; }; + 12149294181E200B008E9BDA /* equalswithdeltaconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = equalswithdeltaconstraint.h; sourceTree = ""; }; + 12149296181E200B008E9BDA /* andexpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = andexpression.h; sourceTree = ""; }; + 12149297181E200B008E9BDA /* expression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = expression.h; sourceTree = ""; }; + 12149298181E200B008E9BDA /* expression_fwd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = expression_fwd.h; sourceTree = ""; }; + 12149299181E200B008E9BDA /* notexpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = notexpression.h; sourceTree = ""; }; + 1214929A181E200B008E9BDA /* orexpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = orexpression.h; sourceTree = ""; }; + 1214929B181E200B008E9BDA /* fulfillsconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fulfillsconstraint.h; sourceTree = ""; }; + 1214929C181E200B008E9BDA /* haslengthconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = haslengthconstraint.h; sourceTree = ""; }; + 1214929D181E200B008E9BDA /* isgreaterthanconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = isgreaterthanconstraint.h; sourceTree = ""; }; + 1214929E181E200B008E9BDA /* islessthanconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = islessthanconstraint.h; sourceTree = ""; }; + 1214929F181E200B008E9BDA /* startswithconstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = startswithconstraint.h; sourceTree = ""; }; + 121492A0181E200B008E9BDA /* exceptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = exceptions.h; sourceTree = ""; }; + 121492A2181E200B008E9BDA /* constraintadapter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = constraintadapter.h; sourceTree = ""; }; + 121492A3181E200B008E9BDA /* constraintlist.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = constraintlist.h; sourceTree = ""; }; + 121492A4181E200B008E9BDA /* expressionbuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = expressionbuilder.h; sourceTree = ""; }; + 121492A5181E200B008E9BDA /* fluent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fluent.h; sourceTree = ""; }; + 121492A7181E200B008E9BDA /* andoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = andoperator.h; sourceTree = ""; }; + 121492A9181E200B008E9BDA /* alloperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = alloperator.h; sourceTree = ""; }; + 121492AA181E200B008E9BDA /* atleastoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = atleastoperator.h; sourceTree = ""; }; + 121492AB181E200B008E9BDA /* atmostoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = atmostoperator.h; sourceTree = ""; }; + 121492AC181E200B008E9BDA /* collectionconstraintevaluator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = collectionconstraintevaluator.h; sourceTree = ""; }; + 121492AD181E200B008E9BDA /* collectionoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = collectionoperator.h; sourceTree = ""; }; + 121492AE181E200B008E9BDA /* exactlyoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = exactlyoperator.h; sourceTree = ""; }; + 121492AF181E200B008E9BDA /* noneoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = noneoperator.h; sourceTree = ""; }; + 121492B0181E200B008E9BDA /* constraintoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = constraintoperator.h; sourceTree = ""; }; + 121492B1181E200B008E9BDA /* notoperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = notoperator.h; sourceTree = ""; }; + 121492B2181E200B008E9BDA /* oroperator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = oroperator.h; sourceTree = ""; }; + 121492B3181E200B008E9BDA /* snowhouse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = snowhouse.h; sourceTree = ""; }; + 121492B4181E200B008E9BDA /* stringize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = stringize.h; sourceTree = ""; }; + 121492B5181E200B008E9BDA /* stringizers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = stringizers.h; sourceTree = ""; }; + 121492C4181E200B008E9BDA /* igloo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = igloo.h; sourceTree = ""; }; + 121492C5181E200B008E9BDA /* igloo_alt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = igloo_alt.h; sourceTree = ""; }; + 121492C6181E200B008E9BDA /* igloo_framework.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = igloo_framework.h; sourceTree = ""; }; + 121492E9181E200B008E9BDA /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main.cpp; path = spec/main.cpp; sourceTree = SOURCE_ROOT; }; + 121492EA181E200B008E9BDA /* rules_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rules_spec.cpp; path = spec/rules_spec.cpp; sourceTree = SOURCE_ROOT; }; + 12C344421822F27700B07BE3 /* transition_map.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = transition_map.h; sourceTree = ""; }; + 12E71794181D02A80051A649 /* specs */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = specs; sourceTree = BUILT_PRODUCTS_DIR; }; + 12E71851181D081C0051A649 /* rule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule.cpp; sourceTree = ""; }; + 12E71852181D081C0051A649 /* rule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule.h; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 12E71791181D02A80051A649 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 1214925C181E200B008E9BDA /* externals */ = { + isa = PBXGroup; + children = ( + 1214925D181E200B008E9BDA /* igloo */, + ); + name = externals; + path = spec/externals; + sourceTree = SOURCE_ROOT; + }; + 1214925D181E200B008E9BDA /* igloo */ = { + isa = PBXGroup; + children = ( + 12149264181E200B008E9BDA /* cotire */, + 12149269181E200B008E9BDA /* igloo */, + ); + path = igloo; + sourceTree = ""; + }; + 12149264181E200B008E9BDA /* cotire */ = { + isa = PBXGroup; + children = ( + 12149265181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx */, + 12149266181E200B008E9BDA /* igloo-tests_CXX_prefix.hxx.gch */, + 12149267181E200B008E9BDA /* snowhouse-tests_CXX_prefix.hxx */, + 12149268181E200B008E9BDA /* snowhouse-tests_CXX_prefix.hxx.gch */, + ); + path = cotire; + sourceTree = ""; + }; + 12149269181E200B008E9BDA /* igloo */ = { + isa = PBXGroup; + children = ( + 1214926A181E200B008E9BDA /* core */, + 12149286181E200B008E9BDA /* external */, + 121492C4181E200B008E9BDA /* igloo.h */, + 121492C5181E200B008E9BDA /* igloo_alt.h */, + 121492C6181E200B008E9BDA /* igloo_framework.h */, + ); + path = igloo; + sourceTree = ""; + }; + 1214926A181E200B008E9BDA /* core */ = { + isa = PBXGroup; + children = ( + 1214926B181E200B008E9BDA /* alternativeregistrationaliases.h */, + 1214926C181E200B008E9BDA /* context.h */, + 1214926D181E200B008E9BDA /* contextbase.h */, + 1214926E181E200B008E9BDA /* contextprovider.h */, + 1214926F181E200B008E9BDA /* contextregistry.h */, + 12149270181E200B008E9BDA /* contextrunner.h */, + 12149271181E200B008E9BDA /* core.h */, + 12149272181E200B008E9BDA /* defaultregistrationaliases.h */, + 12149273181E200B008E9BDA /* outputters */, + 1214927C181E200B008E9BDA /* registration.h */, + 1214927D181E200B008E9BDA /* testlistener.h */, + 1214927E181E200B008E9BDA /* testlisteneraggregator.h */, + 1214927F181E200B008E9BDA /* testlisteners */, + 12149282181E200B008E9BDA /* testresult.h */, + 12149283181E200B008E9BDA /* testresultfactory.h */, + 12149284181E200B008E9BDA /* testresults.h */, + 12149285181E200B008E9BDA /* testrunner.h */, + ); + path = core; + sourceTree = ""; + }; + 12149273181E200B008E9BDA /* outputters */ = { + isa = PBXGroup; + children = ( + 12149274181E200B008E9BDA /* coloredconsoletestresultsoutput.h */, + 12149275181E200B008E9BDA /* consoleoutputcolors.h */, + 12149276181E200B008E9BDA /* defaulttestresultsoutput.h */, + 12149277181E200B008E9BDA /* outputters.h */, + 12149278181E200B008E9BDA /* testresultsoutput.h */, + 12149279181E200B008E9BDA /* visualstudiooutput.h */, + 1214927A181E200B008E9BDA /* xmlwriter.h */, + 1214927B181E200B008E9BDA /* xunitoutput.h */, + ); + path = outputters; + sourceTree = ""; + }; + 1214927F181E200B008E9BDA /* testlisteners */ = { + isa = PBXGroup; + children = ( + 12149280181E200B008E9BDA /* minimalprogresstestlistener.h */, + 12149281181E200B008E9BDA /* testlisteners.h */, + ); + path = testlisteners; + sourceTree = ""; + }; + 12149286181E200B008E9BDA /* external */ = { + isa = PBXGroup; + children = ( + 12149287181E200B008E9BDA /* choices */, + 12149289181E200B008E9BDA /* snowhouse */, + ); + path = external; + sourceTree = ""; + }; + 12149287181E200B008E9BDA /* choices */ = { + isa = PBXGroup; + children = ( + 12149288181E200B008E9BDA /* choices.h */, + ); + path = choices; + sourceTree = ""; + }; + 12149289181E200B008E9BDA /* snowhouse */ = { + isa = PBXGroup; + children = ( + 1214928A181E200B008E9BDA /* snowhouse */, + ); + path = snowhouse; + sourceTree = ""; + }; + 1214928A181E200B008E9BDA /* snowhouse */ = { + isa = PBXGroup; + children = ( + 1214928B181E200B008E9BDA /* assert.h */, + 1214928C181E200B008E9BDA /* assertionexception.h */, + 1214928D181E200B008E9BDA /* assertmacro.h */, + 1214928E181E200B008E9BDA /* constraints */, + 121492A0181E200B008E9BDA /* exceptions.h */, + 121492A1181E200B008E9BDA /* fluent */, + 121492B3181E200B008E9BDA /* snowhouse.h */, + 121492B4181E200B008E9BDA /* stringize.h */, + 121492B5181E200B008E9BDA /* stringizers.h */, + ); + path = snowhouse; + sourceTree = ""; + }; + 1214928E181E200B008E9BDA /* constraints */ = { + isa = PBXGroup; + children = ( + 1214928F181E200B008E9BDA /* constraints.h */, + 12149290181E200B008E9BDA /* containsconstraint.h */, + 12149291181E200B008E9BDA /* endswithconstraint.h */, + 12149292181E200B008E9BDA /* equalsconstraint.h */, + 12149293181E200B008E9BDA /* equalscontainerconstraint.h */, + 12149294181E200B008E9BDA /* equalswithdeltaconstraint.h */, + 12149295181E200B008E9BDA /* expressions */, + 1214929B181E200B008E9BDA /* fulfillsconstraint.h */, + 1214929C181E200B008E9BDA /* haslengthconstraint.h */, + 1214929D181E200B008E9BDA /* isgreaterthanconstraint.h */, + 1214929E181E200B008E9BDA /* islessthanconstraint.h */, + 1214929F181E200B008E9BDA /* startswithconstraint.h */, + ); + path = constraints; + sourceTree = ""; + }; + 12149295181E200B008E9BDA /* expressions */ = { + isa = PBXGroup; + children = ( + 12149296181E200B008E9BDA /* andexpression.h */, + 12149297181E200B008E9BDA /* expression.h */, + 12149298181E200B008E9BDA /* expression_fwd.h */, + 12149299181E200B008E9BDA /* notexpression.h */, + 1214929A181E200B008E9BDA /* orexpression.h */, + ); + path = expressions; + sourceTree = ""; + }; + 121492A1181E200B008E9BDA /* fluent */ = { + isa = PBXGroup; + children = ( + 121492A2181E200B008E9BDA /* constraintadapter.h */, + 121492A3181E200B008E9BDA /* constraintlist.h */, + 121492A4181E200B008E9BDA /* expressionbuilder.h */, + 121492A5181E200B008E9BDA /* fluent.h */, + 121492A6181E200B008E9BDA /* operators */, + ); + path = fluent; + sourceTree = ""; + }; + 121492A6181E200B008E9BDA /* operators */ = { + isa = PBXGroup; + children = ( + 121492A7181E200B008E9BDA /* andoperator.h */, + 121492A8181E200B008E9BDA /* collections */, + 121492B0181E200B008E9BDA /* constraintoperator.h */, + 121492B1181E200B008E9BDA /* notoperator.h */, + 121492B2181E200B008E9BDA /* oroperator.h */, + ); + path = operators; + sourceTree = ""; + }; + 121492A8181E200B008E9BDA /* collections */ = { + isa = PBXGroup; + children = ( + 121492A9181E200B008E9BDA /* alloperator.h */, + 121492AA181E200B008E9BDA /* atleastoperator.h */, + 121492AB181E200B008E9BDA /* atmostoperator.h */, + 121492AC181E200B008E9BDA /* collectionconstraintevaluator.h */, + 121492AD181E200B008E9BDA /* collectionoperator.h */, + 121492AE181E200B008E9BDA /* exactlyoperator.h */, + 121492AF181E200B008E9BDA /* noneoperator.h */, + ); + path = collections; + sourceTree = ""; + }; + 12E716F9181D010E0051A649 = { + isa = PBXGroup; + children = ( + 12E71701181D01890051A649 /* src */, + 12E71796181D02A80051A649 /* spec */, + 12E71795181D02A80051A649 /* Products */, + ); + sourceTree = ""; + }; + 12E71701181D01890051A649 /* src */ = { + isa = PBXGroup; + children = ( + 12E71850181D07EA0051A649 /* rules */, + ); + path = src; + sourceTree = ""; + }; + 12E71795181D02A80051A649 /* Products */ = { + isa = PBXGroup; + children = ( + 12E71794181D02A80051A649 /* specs */, + ); + name = Products; + sourceTree = ""; + }; + 12E71796181D02A80051A649 /* spec */ = { + isa = PBXGroup; + children = ( + 1214925C181E200B008E9BDA /* externals */, + 121492E9181E200B008E9BDA /* main.cpp */, + 121492EA181E200B008E9BDA /* rules_spec.cpp */, + 121306011829FAED00FCF928 /* spec_helper.h */, + ); + name = spec; + path = Specs; + sourceTree = ""; + }; + 12E71850181D07EA0051A649 /* rules */ = { + isa = PBXGroup; + children = ( + 12E71851181D081C0051A649 /* rule.cpp */, + 12E71852181D081C0051A649 /* rule.h */, + 12C344421822F27700B07BE3 /* transition_map.h */, + ); + path = rules; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 12E71793181D02A80051A649 /* specs */ = { + isa = PBXNativeTarget; + buildConfigurationList = 12E7179B181D02A80051A649 /* Build configuration list for PBXNativeTarget "specs" */; + buildPhases = ( + 12E71790181D02A80051A649 /* Sources */, + 12E71791181D02A80051A649 /* Frameworks */, + 12E71792181D02A80051A649 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = specs; + productName = Specs; + productReference = 12E71794181D02A80051A649 /* specs */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 12E716FA181D010E0051A649 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0500; + }; + buildConfigurationList = 12E716FD181D010E0051A649 /* Build configuration list for PBXProject "TreeSitter" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 12E716F9181D010E0051A649; + productRefGroup = 12E71795181D02A80051A649 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 12E71793181D02A80051A649 /* specs */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 12E71790181D02A80051A649 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 12E71853181D081C0051A649 /* rule.cpp in Sources */, + 1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */, + 1214930E181E200B008E9BDA /* main.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 12E716FE181D010E0051A649 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + HEADER_SEARCH_PATHS = ( + "$(inherited)", + /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, + spec/externals/igloo, + src/externals/boost, + ); + USER_HEADER_SEARCH_PATHS = "src/externals/boost spec/externals/igloo"; + }; + name = Debug; + }; + 12E716FF181D010E0051A649 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + HEADER_SEARCH_PATHS = ( + "$(inherited)", + /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, + spec/externals/igloo, + src/externals/boost, + ); + USER_HEADER_SEARCH_PATHS = "src/externals/boost spec/externals/igloo"; + }; + name = Release; + }; + 12E7179C181D02A80051A649 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "c++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + HEADER_SEARCH_PATHS = ( + "$(inherited)", + /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, + spec/externals/igloo, + ); + MACOSX_DEPLOYMENT_TARGET = 10.8; + ONLY_ACTIVE_ARCH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + USER_HEADER_SEARCH_PATHS = ""; + }; + name = Debug; + }; + 12E7179D181D02A80051A649 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "c++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + HEADER_SEARCH_PATHS = ( + "$(inherited)", + /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, + spec/externals/igloo, + ); + MACOSX_DEPLOYMENT_TARGET = 10.8; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + USER_HEADER_SEARCH_PATHS = ""; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 12E716FD181D010E0051A649 /* Build configuration list for PBXProject "TreeSitter" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 12E716FE181D010E0051A649 /* Debug */, + 12E716FF181D010E0051A649 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 12E7179B181D02A80051A649 /* Build configuration list for PBXNativeTarget "specs" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 12E7179C181D02A80051A649 /* Debug */, + 12E7179D181D02A80051A649 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 12E716FA181D010E0051A649 /* Project object */; +} diff --git a/spec/externals/igloo b/spec/externals/igloo new file mode 160000 index 00000000..d8c6aa24 --- /dev/null +++ b/spec/externals/igloo @@ -0,0 +1 @@ +Subproject commit d8c6aa24de88740c6f1783281249eb18f858deaf diff --git a/spec/main.cpp b/spec/main.cpp new file mode 100644 index 00000000..5134b938 --- /dev/null +++ b/spec/main.cpp @@ -0,0 +1,8 @@ +#include + +using namespace igloo; + +int main(int argc, char *argv[]) +{ + return TestRunner::RunAllTests(argc, argv); +} \ No newline at end of file diff --git a/spec/rules_spec.cpp b/spec/rules_spec.cpp new file mode 100644 index 00000000..ea35b2f3 --- /dev/null +++ b/spec/rules_spec.cpp @@ -0,0 +1,53 @@ +#include "rule.h" +#include "transition_map.h" +#include + +using namespace std; +using namespace igloo; +using namespace tree_sitter::rules; + +Describe(Rules) { + Describe(transitions) { + Symbol symbol1 = Symbol(1); + Symbol symbol2 = Symbol(2); + Symbol symbol3 = Symbol(3); + + It(handles_symbols) { + AssertThat( + symbol1.transitions(), + EqualsContainer(TransitionMap( + { symbol1.copy() }, + { new Blank() } + ), TransitionMap::elements_equal)); + } + + It(handles_choices) { + AssertThat( + Choice(symbol1, symbol2).transitions(), + EqualsContainer(TransitionMap( + { symbol1.copy(), symbol2.copy() }, + { new Blank(), new Blank() } + ), TransitionMap::elements_equal)); + } + + It(handles_sequences) { + AssertThat( + Seq(symbol1, symbol2).transitions(), + EqualsContainer(TransitionMap( + { symbol1.copy() }, + { symbol2.copy() } + ), TransitionMap::elements_equal)); + } + + It(handles_choices_with_common_starting_symbols) { + AssertThat( + Choice( + Seq(symbol1, symbol2), + Seq(symbol1, symbol3)).transitions(), + EqualsContainer(TransitionMap( + { symbol1.copy() }, + { new Choice(symbol2, symbol3) } + ), TransitionMap::elements_equal)); + } + }; +}; diff --git a/spec_helper.h b/spec_helper.h new file mode 100644 index 00000000..282dea03 --- /dev/null +++ b/spec_helper.h @@ -0,0 +1,22 @@ +#ifndef TreeSitter_SpecHelper_h +#define TreeSitter_SpecHelper_h + +#include "igloo/igloo_alt.h" +#include "transition_map.h" +#include "rule.h" + +namespace snowhouse { + template<> + std::string Stringize(const tree_sitter::rules::TransitionMap &map) { + std::string result("["); + bool started = false; + for (auto pair : map) { + if (started) result += ", "; + result += (pair.first->to_string() + " => " + pair.second->to_string()); + started = true; + } + return result + "]"; + } +} + +#endif diff --git a/src/rules/rule.cpp b/src/rules/rule.cpp new file mode 100644 index 00000000..d09d589e --- /dev/null +++ b/src/rules/rule.cpp @@ -0,0 +1,95 @@ +#include "rule.h" +#include "spec_helper.h" +#include "transition_map.h" + +using namespace std; + +namespace tree_sitter { + namespace rules { + + // Constructors + Blank::Blank() {} + Symbol::Symbol(int id) : id(id) {}; + Seq::Seq(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {}; + Seq::Seq(const Rule *left, const Rule *right) : left(left), right(right) {}; + Seq::Seq(shared_ptr left, shared_ptr right) : left(left), right(right) {}; + Choice::Choice(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {}; + Choice::Choice(const Rule *left, const Rule *right) : left(left), right(right) {}; + Choice::Choice(shared_ptr left, shared_ptr right) : left(left), right(right) {}; + + // Transitions + TransitionMap Blank::transitions() const { + return TransitionMap(); + } + + TransitionMap Symbol::transitions() const { + return TransitionMap({ copy() }, { new Blank() }); + } + + TransitionMap Choice::transitions() const { + auto result = left->transitions(); + result.merge(right->transitions(), [&](const Rule &left, const Rule &right) { + return new Choice(left, right); + }); + return result; + } + + TransitionMap Seq::transitions() const { + return TransitionMap({ left->copy() }, { right->copy() }); + } + + // Equality + bool Blank::operator==(const Rule &rule) const { + return dynamic_cast(&rule) != NULL; + } + + bool Symbol::operator==(const Rule &rule) const { + const Symbol *other = dynamic_cast(&rule); + return (other != NULL) && (other->id == id); + } + + bool Choice::operator==(const Rule &rule) const { + const Choice *other = dynamic_cast(&rule); + return (other != NULL) && (*other->left == *left) && (*other->right == *right); + } + + bool Seq::operator==(const Rule &rule) const { + const Seq *other = dynamic_cast(&rule); + return (other != NULL) && (*other->left == *left) && (*other->right == *right); + } + + // Copying + Blank * Blank::copy() const { + return new Blank(); + } + + Symbol * Symbol::copy() const { + return new Symbol(id); + } + + Choice * Choice::copy() const { + return new Choice(left, right); + } + + Seq * Seq::copy() const { + return new Seq(left, right); + } + + // Description + string Blank::to_string() const { + return "blank"; + } + + string Symbol::to_string() const { + return string(std::to_string(id)); + } + + string Choice::to_string() const { + return string("(choice ") + left->to_string() + " " + right->to_string() + ")"; + } + + string Seq::to_string() const { + return string("(seq ") + left->to_string() + " " + right->to_string() + ")"; + } + } +} \ No newline at end of file diff --git a/src/rules/rule.h b/src/rules/rule.h new file mode 100644 index 00000000..72455b84 --- /dev/null +++ b/src/rules/rule.h @@ -0,0 +1,68 @@ +#ifndef __TreeSitter__Rule__ +#define __TreeSitter__Rule__ + +#include + +namespace tree_sitter { + namespace rules { + template class TransitionMap; + + class Rule { + public: + virtual TransitionMap transitions() const = 0; + virtual Rule * copy() const = 0; + virtual bool operator==(const Rule& other) const = 0; + virtual std::string to_string() const = 0; + }; + + class Blank : public Rule { + public: + Blank(); + TransitionMap transitions() const; + Blank * copy() const; + bool operator==(const Rule& other) const; + std::string to_string() const; + }; + + class Symbol : public Rule { + public: + Symbol(int id); + TransitionMap transitions() const; + Symbol * copy() const; + bool operator==(const Rule& other) const; + std::string to_string() const; + private: + int id; + }; + + class Choice : public Rule { + public: + Choice(const Rule &left, const Rule &right); + Choice(const Rule *left, const Rule *right); + Choice(std::shared_ptr left, std::shared_ptr right); + TransitionMap transitions() const; + Choice * copy() const; + bool operator==(const Rule& other) const; + std::string to_string() const; + private: + std::shared_ptr left; + std::shared_ptr right; + }; + + class Seq : public Rule { + public: + Seq(const Rule &left, const Rule &right); + Seq(const Rule *left, const Rule *right); + Seq(std::shared_ptr left, std::shared_ptr right); + TransitionMap transitions() const; + Seq * copy() const; + bool operator==(const Rule& other) const; + std::string to_string() const; + private: + std::shared_ptr left; + std::shared_ptr right; + }; + } +} + +#endif diff --git a/src/rules/transition_map.h b/src/rules/transition_map.h new file mode 100644 index 00000000..c6a2b50e --- /dev/null +++ b/src/rules/transition_map.h @@ -0,0 +1,94 @@ +#ifndef __TreeSitter__TransitionSet__ +#define __TreeSitter__TransitionSet__ + +#include +#include +#include "rule.h" + +namespace tree_sitter { + namespace rules { + class Rule; + + template + class TransitionMap { + public: + typedef std::shared_ptr rule_ptr; + typedef std::shared_ptr mapped_ptr; + typedef std::pair pair_type; + typedef std::vector contents_type; + + static bool elements_equal(const pair_type &left, const pair_type &right) { + return (*left.first == *right.first) && (*left.second == *right.second); + } + + TransitionMap() : contents(contents_type()) {}; + + TransitionMap(std::initializer_list keys, std::initializer_list values) : TransitionMap() { + auto value_iter(values.begin()); + for (auto key_iter(keys.begin()); key_iter != keys.end(); ++key_iter) { + add(*key_iter, *value_iter); + ++value_iter; + } + } + + typedef typename contents_type::const_iterator const_iterator; + typedef typename contents_type::iterator iterator; + + iterator begin() { return contents.begin(); } + iterator end() { return contents.end(); } + const_iterator begin() const { return contents.begin(); } + const_iterator end() const { return contents.end(); } + + void add(const Rule *on_rule, const MappedType *to_value) { + rule_ptr key(on_rule); + mapped_ptr value(to_value); + contents.push_back(pair_type(key, value)); + } + + void add(rule_ptr on_rule, mapped_ptr to_value) { + rule_ptr key(on_rule); + mapped_ptr value(to_value); + contents.push_back(pair_type(key, value)); + } + + size_t size() const { + return contents.size(); + } + + mapped_ptr operator[](Rule const &on_rule) const { + pair_type *pair = pair_for_key(on_rule); + if (pair) + return pair->second; + else + return NULL; + } + + mapped_ptr operator[](int i) const { + return contents[i].second; + } + + void merge(const TransitionMap &other, std::function merge_fn) { + for (pair_type other_pair : other) { + pair_type *current_pair = pair_for_key(*other_pair.first); + if (current_pair) + current_pair->second = mapped_ptr(merge_fn(*current_pair->second, *other_pair.second)); + else + add(other_pair.first, other_pair.second); + } + } + + private: + pair_type * pair_for_key(Rule const &key) { + for (int i = 0; i < contents.size(); i++) { + pair_type *pair = &contents[i]; + if (*pair->first == key) return pair; + } + return NULL; + } + + contents_type contents; + }; + } +} + +#endif \ No newline at end of file