[gnome-builder/wip/chergert/templates: 27/27] wip: templates
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/chergert/templates: 27/27] wip: templates
- Date: Sat, 23 Jan 2016 19:41:24 +0000 (UTC)
commit fe8207fe25ba465f93e1f678e027b8807974f84f
Author: Christian Hergert <christian hergert me>
Date: Sat Jan 23 11:35:12 2016 -0800
wip: templates
Makefile.am | 11 +-
configure.ac | 15 +
contrib/Makefile.am | 1 +
contrib/tmpl/Makefile.am | 161 +++
contrib/tmpl/configure.ac.tmpl | 203 ++++
contrib/tmpl/tmpl-branch-node.c | 266 +++++
contrib/tmpl/tmpl-branch-node.h | 43 +
contrib/tmpl/tmpl-condition-node.c | 178 +++
contrib/tmpl/tmpl-condition-node.h | 40 +
contrib/tmpl/tmpl-debug.h.in | 80 ++
contrib/tmpl/tmpl-enums.c.in | 42 +
contrib/tmpl/tmpl-enums.h.in | 24 +
contrib/tmpl/tmpl-error.c | 25 +
contrib/tmpl/tmpl-error.h | 58 +
contrib/tmpl/tmpl-expr-eval.c | 1153 ++++++++++++++++++++
contrib/tmpl/tmpl-expr-node.c | 112 ++
contrib/tmpl/tmpl-expr-node.h | 36 +
contrib/tmpl/tmpl-expr-parser-private.h | 48 +
contrib/tmpl/tmpl-expr-parser.y | 200 ++++
contrib/tmpl/tmpl-expr-private.h | 158 +++
contrib/tmpl/tmpl-expr-scanner.l | 179 +++
contrib/tmpl/tmpl-expr-types.h | 93 ++
contrib/tmpl/tmpl-expr.c | 337 ++++++
contrib/tmpl/tmpl-expr.h | 69 ++
contrib/tmpl/tmpl-gi-private.h | 38 +
contrib/tmpl/tmpl-gi.c | 393 +++++++
contrib/tmpl/tmpl-glib.h | 38 +
contrib/tmpl/tmpl-iter-node.c | 189 ++++
contrib/tmpl/tmpl-iter-node.h | 42 +
contrib/tmpl/tmpl-iterator.c | 142 +++
contrib/tmpl/tmpl-iterator.h | 54 +
contrib/tmpl/tmpl-lexer.c | 199 ++++
contrib/tmpl/tmpl-lexer.h | 49 +
contrib/tmpl/tmpl-node.c | 339 ++++++
contrib/tmpl/tmpl-node.h | 66 ++
contrib/tmpl/tmpl-parser.c | 267 +++++
contrib/tmpl/tmpl-parser.h | 48 +
contrib/tmpl/tmpl-scope.c | 168 +++
contrib/tmpl/tmpl-scope.h | 46 +
contrib/tmpl/tmpl-symbol.c | 237 ++++
contrib/tmpl/tmpl-symbol.h | 54 +
contrib/tmpl/tmpl-template-locator.c | 207 ++++
contrib/tmpl/tmpl-template-locator.h | 57 +
contrib/tmpl/tmpl-template.c | 540 +++++++++
contrib/tmpl/tmpl-template.h | 76 ++
contrib/tmpl/tmpl-text-node.c | 105 ++
contrib/tmpl/tmpl-text-node.h | 37 +
contrib/tmpl/tmpl-token-input-stream.c | 307 ++++++
contrib/tmpl/tmpl-token-input-stream.h | 43 +
contrib/tmpl/tmpl-token.c | 181 +++
contrib/tmpl/tmpl-token.h | 57 +
contrib/tmpl/tmpl-util-private.h | 36 +
contrib/tmpl/tmpl-util.c | 171 +++
data/Makefile.am | 6 +
data/template-glib-1.0.pc.in | 11 +
data/theme/shared.css | 25 +
libide/Makefile.am | 13 +-
libide/ide.h | 2 +
libide/template/ide-project-template.c | 135 +++
libide/template/ide-project-template.h | 67 ++
libide/template/ide-template-base.c | 554 ++++++++++
libide/template/ide-template-base.h | 56 +
libide/template/ide-template-provider.c | 53 +
libide/template/ide-template-provider.h | 41 +
plugins/Makefile.am | 2 +
plugins/create-project/Makefile.am | 46 +
plugins/create-project/configure.ac | 12 +
plugins/create-project/create-project.plugin | 10 +
plugins/create-project/gbp-create-project-plugin.c | 30 +
plugins/create-project/gbp-create-project-tool.c | 352 ++++++
plugins/create-project/gbp-create-project-tool.h | 32 +
.../gbp-create-project.gresource.xml | 5 +
plugins/library-template/Makefile.am | 18 +
plugins/library-template/configure.ac | 11 +
plugins/library-template/library-template.plugin | 11 +
.../library-template/library_template/__init__.py | 86 ++
.../library-template/library_template/configure.ac | 2 +
plugins/vala-pack/Makefile.am | 3 +
78 files changed, 9292 insertions(+), 9 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index aa02f9d..a5b435a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,8 +1,5 @@
SUBDIRS = . build contrib data libide src plugins tools po tests doc
-pkgconfigdir = $(libdir)/gnome-builder/pkgconfig
-pkgconfig_DATA = $(top_builddir)/data/libide-1.0.pc
-
INTLTOOL_FILES = \
intltool-extract.in \
intltool-merge.in \
@@ -75,7 +72,7 @@ run:
GB_IN_TREE_PLUGINS=1 \
GB_IN_TREE_FONTS=1 \
GB_IN_TREE_STYLE_SCHEMES=1 \
- GI_TYPELIB_PATH="libide:$(GI_TYPELIB_PATH)" \
+ GI_TYPELIB_PATH="libide:contrib/tmpl:$(GI_TYPELIB_PATH)" \
GOBJECT_DEBUG=instance-count \
PATH=$(top_builddir)/src:${PATH} \
$(LIBTOOL) --mode=execute gdb -ex run --args src/gnome-builder -vvvv -s
@@ -85,7 +82,7 @@ strace:
GB_IN_TREE_PLUGINS=1 \
GB_IN_TREE_FONTS=1 \
GB_IN_TREE_STYLE_SCHEMES=1 \
- GI_TYPELIB_PATH="libide:$(GI_TYPELIB_PATH)" \
+ GI_TYPELIB_PATH="libide:contrib/tmpl:$(GI_TYPELIB_PATH)" \
GOBJECT_DEBUG=instance-count \
PATH=$(top_builddir)/src:${PATH} \
$(LIBTOOL) --mode=execute strace -T src/gnome-builder -vvvv -s
@@ -95,7 +92,7 @@ debug:
GB_IN_TREE_PLUGINS=1 \
GB_IN_TREE_FONTS=1 \
GB_IN_TREE_STYLE_SCHEMES=1 \
- GI_TYPELIB_PATH="libide:$(GI_TYPELIB_PATH)" \
+ GI_TYPELIB_PATH="libide:contrib/tmpl:$(GI_TYPELIB_PATH)" \
G_DEBUG=fatal-criticals \
GOBJECT_DEBUG=instance-count \
PATH=$(top_builddir)/src:${PATH} \
@@ -106,7 +103,7 @@ valgrind:
GB_IN_TREE_PLUGINS=1 \
GB_IN_TREE_FONTS=1 \
GB_IN_TREE_STYLE_SCHEMES=1 \
- GI_TYPELIB_PATH="libide:$(GI_TYPELIB_PATH)" \
+ GI_TYPELIB_PATH="libide:contrib/tmpl:$(GI_TYPELIB_PATH)" \
G_DEBUG=fatal-criticals \
G_SLICE=always-malloc \
PATH=$(top_builddir)/src:${PATH} \
diff --git a/configure.ac b/configure.ac
index 66b8bb3..78c54ab 100644
--- a/configure.ac
+++ b/configure.ac
@@ -66,6 +66,8 @@ AM_PROG_CC_C_O
AC_PROG_CXX
AM_PROG_VALAC
AC_PROG_INSTALL
+AM_PROG_LEX
+AC_PROG_YACC
AC_PATH_PROG([GLIB_GENMARSHAL], [glib-genmarshal])
AC_PATH_PROG([GLIB_MKENUMS], [glib-mkenums])
AC_PATH_PROG([GLIB_COMPILE_RESOURCES], [glib-compile-resources])
@@ -156,6 +158,7 @@ m4_define([gtk_required_version], [3.19.7])
m4_define([glib_required_version], [2.47.4])
m4_define([gtksourceview_required_version], [3.19.4])
m4_define([ggit_required_version], [0.23.7])
+m4_define([gobject_introspection_version], [1.47.1])
m4_define([pygobject_required_version], [3.19.3])
m4_define([libxml_required_version], [2.9.0])
m4_define([pangoft2_required_version], [1.38.0])
@@ -184,6 +187,8 @@ PKG_CHECK_MODULES(PYGOBJECT,[pygobject-3.0 >= pygobject_required_version],
[have_pygobject=no])
PKG_CHECK_MODULES(RG, [gtk+-3.0 >= gtk_required_version])
PKG_CHECK_MODULES(SEARCH, [glib-2.0 >= glib_required_version])
+PKG_CHECK_MODULES(TMPL, [gio-2.0 >= glib_required_version
+ gobject-introspection-1.0 >= gobject_introspection_version])
PKG_CHECK_MODULES(XML, [gio-2.0 >= glib_required_version
libxml-2.0 >= libxml_required_version])
@@ -231,6 +236,7 @@ m4_include([plugins/c-pack/configure.ac])
m4_include([plugins/clang/configure.ac])
m4_include([plugins/command-bar/configure.ac])
m4_include([plugins/contributing/configure.ac])
+m4_include([plugins/create-project/configure.ac])
m4_include([plugins/ctags/configure.ac])
m4_include([plugins/devhelp/configure.ac])
m4_include([plugins/file-search/configure.ac])
@@ -241,6 +247,7 @@ m4_include([plugins/gnome-code-assistance/configure.ac])
m4_include([plugins/html-completion/configure.ac])
m4_include([plugins/html-preview/configure.ac])
m4_include([plugins/jedi/configure.ac])
+m4_include([plugins/library-template/configure.ac])
m4_include([plugins/mingw/configure.ac])
m4_include([plugins/project-tree/configure.ac])
m4_include([plugins/python-gi-imports-completion/configure.ac])
@@ -423,6 +430,8 @@ AC_CONFIG_FILES([
contrib/nautilus/Makefile
contrib/rg/Makefile
contrib/search/Makefile
+ contrib/tmpl/Makefile
+ contrib/tmpl/tmpl-debug.h
contrib/xml/Makefile
libide/ide-debug.h
@@ -438,6 +447,7 @@ AC_CONFIG_FILES([
data/icons/hicolor/Makefile
data/libide-1.0.pc
data/style-schemes/Makefile
+ data/template-glib-1.0.pc
doc/Makefile
doc/examples/Makefile
@@ -498,6 +508,7 @@ echo " GNOME Code Assistance ................ : ${enable_gnome_code_assistance_
echo " HTML Autocompletion .................. : ${enable_html_completion_plugin}"
echo " HTML and Markdown Preview ............ : ${enable_html_preview_plugin}"
echo " MinGW ................................ : ${enable_mingw_plugin}"
+echo " Project Creation ..................... : ${enable_create_project_plugin}"
echo " Project Tree ......................... : ${enable_project_tree_plugin}"
echo " Python GObject Introspection ......... : ${enable_python_gi_imports_completion_plugin}"
echo " Python Jedi Autocompletion ........... : ${enable_jedi_plugin}"
@@ -510,4 +521,8 @@ echo " Terminal ............................. : ${enable_terminal_plugin}"
echo " Vala Language Pack ................... : ${enable_vala_pack_plugin}"
echo " XML Language Pack .................... : ${enable_xml_pack_plugin}"
echo ""
+echo " Templates"
+echo ""
+echo " Library .............................. : ${enable_library_template_plugin}"
+echo ""
diff --git a/contrib/Makefile.am b/contrib/Makefile.am
index d01f238..dcfefa4 100644
--- a/contrib/Makefile.am
+++ b/contrib/Makefile.am
@@ -6,6 +6,7 @@ SUBDIRS = \
nautilus \
rg \
search \
+ tmpl \
xml \
$(NULL)
diff --git a/contrib/tmpl/Makefile.am b/contrib/tmpl/Makefile.am
new file mode 100644
index 0000000..78eeb5c
--- /dev/null
+++ b/contrib/tmpl/Makefile.am
@@ -0,0 +1,161 @@
+DISTCLEANFILES =
+CLEANFILES =
+BUILT_SOURCES =
+
+pkglib_LTLIBRARIES = libtemplate-glib-1.0.la
+
+AM_CPPFLAGS = \
+ -D_GNU_SOURCE \
+ -DTMPL_GLIB_COMPILATION
+
+EXTRA_DIST = \
+ tmpl-expr-parser.c \
+ tmpl-expr-parser.h \
+ tmpl-expr-scanner.c \
+ tmpl-expr-scanner.h
+
+pkgincludedir = $(includedir)/template-glib-1.0
+pkginclude_HEADERS = \
+ tmpl-error.h \
+ tmpl-expr-types.h \
+ tmpl-expr.h \
+ tmpl-glib.h \
+ tmpl-scope.h \
+ tmpl-symbol.h \
+ tmpl-template-locator.h \
+ tmpl-template.h \
+ $(NULL)
+
+libtemplate_glib_1_0_la_CFLAGS = $(TMPL_CFLAGS)
+libtemplate_glib_1_0_la_LIBADD = $(TMPL_LIBS)
+libtemplate_glib_1_0_la_SOURCES = \
+ $(pkginclude_HEADERS) \
+ tmpl-branch-node.c \
+ tmpl-branch-node.h \
+ tmpl-condition-node.c \
+ tmpl-condition-node.h \
+ tmpl-debug.h \
+ tmpl-enums.c \
+ tmpl-enums.h \
+ tmpl-error.c \
+ tmpl-expr-eval.c \
+ tmpl-expr-node.c \
+ tmpl-expr-node.h \
+ tmpl-expr-parser-private.h \
+ tmpl-expr-parser.y \
+ tmpl-expr-private.h \
+ tmpl-expr-scanner.l \
+ tmpl-expr.c \
+ tmpl-gi-private.h \
+ tmpl-gi.c \
+ tmpl-iter-node.c \
+ tmpl-iter-node.h \
+ tmpl-iterator.c \
+ tmpl-iterator.h \
+ tmpl-lexer.c \
+ tmpl-lexer.h \
+ tmpl-node.c \
+ tmpl-node.h \
+ tmpl-parser.c \
+ tmpl-parser.h \
+ tmpl-scope.c \
+ tmpl-symbol.c \
+ tmpl-template-locator.c \
+ tmpl-template.c \
+ tmpl-text-node.c \
+ tmpl-text-node.h \
+ tmpl-token-input-stream.c \
+ tmpl-token-input-stream.h \
+ tmpl-token.c \
+ tmpl-token.h \
+ tmpl-util-private.h \
+ tmpl-util.c \
+ $(NULL)
+
+glib_enum_h = tmpl-enums.h
+glib_enum_c = tmpl-enums.c
+glib_enum_headers = \
+ tmpl-error.h \
+ tmpl-expr-types.h \
+ $(NULL)
+include $(top_srcdir)/build/autotools/Makefile.am.enums
+
+if HAVE_INTROSPECTION
+-include $(INTROSPECTION_MAKEFILE)
+
+INTROSPECTION_GIRS =
+INTROSPECTION_SCANNER_ARGS = --add-include-path=$(srcdir) --warn-all
+INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir)
+
+introspection_sources = \
+ tmpl-enums.c \
+ tmpl-enums.h \
+ tmpl-error.h \
+ tmpl-expr.c \
+ tmpl-expr.h \
+ tmpl-expr-types.h \
+ tmpl-scope.c \
+ tmpl-scope.h \
+ tmpl-symbol.c \
+ tmpl-symbol.h \
+ tmpl-template-locator.c \
+ tmpl-template-locator.h \
+ tmpl-template.c \
+ tmpl-template.h \
+ $(NULL)
+
+Template-1.0.gir: libtemplate-glib-1.0.la
+Template_1_0_gir_INCLUDES = Gio-2.0
+Template_1_0_gir_CFLAGS = $(libtemplate_glib_1_0_la_CFLAGS)
+Template_1_0_gir_LIBS = libtemplate-glib-1.0.la
+Template_1_0_gir_FILES = $(introspection_sources)
+Template_1_0_gir_SCANNERFLAGS = \
+ -n Template \
+ --identifier-prefix Tmpl \
+ --symbol-prefix tmpl \
+ -DTMPL_GLIB_COMPILATION \
+ $(NULL)
+INTROSPECTION_GIRS += Template-1.0.gir
+
+pkggirdir = $(datadir)/gnome-builder/gir-1.0
+pkggir_DATA = $(INTROSPECTION_GIRS)
+
+pkgtypelibdir = $(libdir)/gnome-builder/girepository-1.0
+pkgtypelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib)
+
+CLEANFILES += $(gir_DATA) $(typelib_DATA)
+endif
+
+if ENABLE_VAPIGEN
+-include $(VAPIGEN_MAKEFILE)
+
+template-glib-1.0.vapi: Template-1.0.gir
+
+VAPIGEN_VAPIS = template-glib-1.0.vapi
+
+template_glib_1_0_vapi_DEPS = gio-2.0
+template_glib_1_0_vapi_METADATADIRS = $(srcdir)
+template_glib_1_0_vapi_FILES = Template-1.0.gir
+
+template-glib-1.0.deps: Makefile
+ $(AM_V_GEN) echo $(template_glib_1_0_vapi_DEPS) | tr ' ' '\n' > $@
+
+vapidir = $(datadir)/gnome-builder/vapi
+vapi_DATA = $(VAPIGEN_VAPIS) $(VAPIGEN_VAPIS:.vapi=.deps)
+
+EXTRA_DIST += template-glib-1.0.deps
+
+DISTCLEANFILES += $(vapi_DATA)
+endif
+
+GITIGNOREFILES = \
+ tmpl-expr-parser.c \
+ tmpl-expr-parser.h \
+ tmpl-expr-scanner.c \
+ tmpl-expr-scanner.h \
+ tmpl-debug.h \
+ *.typelib \
+ *.gir \
+ lex.*
+
+-include $(top_srcdir)/git.mk
diff --git a/contrib/tmpl/configure.ac.tmpl b/contrib/tmpl/configure.ac.tmpl
new file mode 100644
index 0000000..f1df63b
--- /dev/null
+++ b/contrib/tmpl/configure.ac.tmpl
@@ -0,0 +1,203 @@
+AC_PREREQ([{{autoconf_required_version}}])
+
+dnl ***********************************************************************
+dnl Define Versioning Information
+dnl ***********************************************************************
+m4_define([major_version],[{{version_major}}])
+m4_define([minor_version],[{{version_minor}}])
+m4_define([micro_version],[{{version_micro}}])
+m4_define([version],[major_version.minor_version.micro_version])
+m4_define([interface_age],[0])
+m4_define([bugreport_url],[{{bugreport_url}}])
+m4_define([debug_default],[m4_if(m4_eval(minor_version % 2),[1],[yes],[minimum])])
+
+
+dnl ***********************************************************************
+dnl Initialize Autoconf
+dnl ***********************************************************************
+AC_INIT([{{name}}],[version],[bugreport_url])
+AC_CONFIG_HEADERS([config.h])
+AC_CONFIG_SRCDIR([configure.ac])
+AC_CONFIG_MACRO_DIR([build-aux])
+AC_CONFIG_AUX_DIR([build-aux])
+AC_SUBST(ACLOCAL_AMFLAGS, "-I build-aux")
+AC_CANONICAL_HOST
+
+
+dnl ***********************************************************************
+dnl Export version information
+dnl ***********************************************************************
+MAJOR_VERSION=major_version
+MINOR_VERSION=minor_version
+MICRO_VERSION=micro_version
+AC_SUBST(MAJOR_VERSION)
+AC_SUBST(MINOR_VERSION)
+AC_SUBST(MICRO_VERSION)
+
+
+dnl ***********************************************************************
+dnl Initialize Automake
+dnl ***********************************************************************
+AM_SILENT_RULES([yes])
+AM_INIT_AUTOMAKE([1.11 foreign subdir-objects tar-ustar no-dist-gzip dist-xz])
+AM_MAINTAINER_MODE([enable])
+
+
+dnl ***********************************************************************
+dnl Internationalization
+dnl ***********************************************************************
+IT_PROG_INTLTOOL([0.50.1])
+GETTEXT_PACKAGE=AC_PACKAGE_TARNAME
+AC_SUBST(GETTEXT_PACKAGE)
+AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"], [GETTEXT package name])
+AM_GLIB_GNU_GETTEXT
+
+
+dnl ***********************************************************************
+dnl Check for Required Programs
+dnl ***********************************************************************
+AC_PROG_CC
+{{if enable_cplusplus}}
+AC_PROG_CXX
+{{end}}
+AC_PROG_INSTALL
+AC_PATH_PROG([GLIB_GENMARSHAL], [glib-genmarshal])
+AC_PATH_PROG([GLIB_MKENUMS], [glib-mkenums])
+AC_PATH_PROG([GLIB_COMPILE_RESOURCES], [glib-compile-resources])
+GLIB_GSETTINGS
+PKG_PROG_PKG_CONFIG([0.22])
+{{if enable_gobject_introspection}}
+GOBJECT_INTROSPECTION_CHECK([1.42.0])
+{{end}}
+{{if enable_vala}}
+AM_PROG_VALAC
+VAPIGEN_CHECK
+{{end}}
+{{if enable_appstream}}
+APPSTREAM_XML
+{{end}}
+
+
+{{if enable_c11}}
+dnl ***********************************************************************
+dnl Ensure C11 is Supported
+dnl ***********************************************************************
+AX_CHECK_COMPILE_FLAG([-std=gnu11],
+ [CFLAGS="$CFLAGS -std=gnu11"],
+ [AC_MSG_ERROR([C compiler cannot compile GNU C11 code])])
+
+
+{{end}}
+dnl ***********************************************************************
+dnl Setup Debug and Tracing Support
+dnl ***********************************************************************
+AC_ARG_ENABLE(debug,
+ AS_HELP_STRING([--enable-debug=@<:@no/minimum/yes@:>@],
+ [turn on debugging @<:@default=debug_default@:>@]),
+ ,
+ enable_debug=debug_default)
+AS_CASE(["$enable_debug"],
+ [yes],[
+ DEBUG_CFLAGS="$DEBUG_CFLAGS -O0"
+ DEBUG_CFLAGS="$DEBUG_CFLAGS -g"
+ ],
+ [minimum],[
+ DEBUG_CFLAGS="$DEBUG_CFLAGS -DG_DISABLE_CAST_CHECKS"
+ ],
+ [no],[
+ DEBUG_CFLAGS="$DEBUG_CFLAGS -DG_DISABLE_ASSERT"
+ DEBUG_CFLAGS="$DEBUG_CFLAGS -DG_DISABLE_CHECKS"
+ DEBUG_CFLAGS="$DEBUG_CFLAGS -DG_DISABLE_CAST_CHECKS"
+ ],
+ [])
+AC_SUBST(DEBUG_CFLAGS)
+
+
+dnl ***********************************************************************
+dnl Check for Required Packages
+dnl ***********************************************************************
+m4_define([glib_required_version],[{{glib_required_version}}])
+
+PKG_CHECK_MODULES({{NAME}}, [
+ gobject-2.0 >= glib_required_version
+ gio-2.0 >= glib_required_version
+])
+
+
+dnl ***********************************************************************
+dnl Initialize Libtool
+dnl ***********************************************************************
+LT_PREREQ([2.2])
+LT_INIT
+
+
+{{if enable_compiler_flags}}
+dnl ***********************************************************************
+dnl Additional C Compiler Flags
+dnl ***********************************************************************
+AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option], [
+ ax_compiler_flags_test="-Werror=unknown-warning-option"
+], [
+ ax_compiler_flags_test=""
+])
+AX_APPEND_COMPILE_FLAGS([ \
+ -Wall \
+ -Wcast-align \
+ -Wdeclaration-after-statement \
+ -Wempty-body \
+ -Werror=format-security \
+ -Werror=format=2 \
+ -Wextra \
+ -Wformat \
+ -Wformat-nonliteral \
+ -Wformat-security \
+ -Winit-self \
+ -Wmisleading-indentation \
+ -Wmissing-include-dirs \
+ -Wshift-negative-value \
+ -Wnested-externs \
+ -Wno-missing-field-initializers \
+ -Wno-sign-compare \
+ -Wno-strict-aliasing \
+ -Wno-uninitialized \
+ -Wno-unused-parameter \
+ -Wpointer-arith \
+ -Wredundant-decls \
+ -Wreturn-type \
+ -Wshadow \
+ -Wswitch-default \
+ -Wswitch-enum \
+ -Wundef \
+ -Wuninitialized \
+], [], [$ax_compiler_flags_test])
+AC_C_CONST
+
+
+{{end}}
+{{if enable_gtk_doc}}
+dnl ***********************************************************************
+dnl Support for gtk-doc Documentation Engine
+dnl ***********************************************************************
+GTK_DOC_CHECK([1.11],[--flavour no-tmpl])
+AM_CONDITIONAL(ENABLE_GTK_DOC, test "x$enable_gtk_doc" = "xyes")
+AC_ARG_ENABLE(doc-cross-references,
+ AS_HELP_STRING([--disable-doc-cross-references],
+ [cross reference symbols from other libraries @<:@default=yes@:>@]),
+ enable_doc_cross_references=$enableval,
+ enable_doc_cross_references=yes)
+AM_CONDITIONAL(ENABLE_DOC_CROSS_REFERENCES, test x$enable_doc_cross_references != xno)
+
+
+{{end}}
+dnl ***********************************************************************
+dnl Process .in Files
+dnl ***********************************************************************
+AC_CONFIG_FILES([
+ Makefile
+])
+AC_OUTPUT
+
+
+echo ""
+echo " ${PACKAGE} - ${VERSION}"
+echo ""
diff --git a/contrib/tmpl/tmpl-branch-node.c b/contrib/tmpl/tmpl-branch-node.c
new file mode 100644
index 0000000..10994ad
--- /dev/null
+++ b/contrib/tmpl/tmpl-branch-node.c
@@ -0,0 +1,266 @@
+/* tmpl-branch-node.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-branch-node"
+
+#include "tmpl-branch-node.h"
+#include "tmpl-condition-node.h"
+#include "tmpl-debug.h"
+#include "tmpl-error.h"
+#include "tmpl-util-private.h"
+
+struct _TmplBranchNode
+{
+ TmplNode parent_instance;
+
+ TmplNode *if_branch;
+ GPtrArray *children;
+};
+
+G_DEFINE_TYPE (TmplBranchNode, tmpl_branch_node, TMPL_TYPE_NODE)
+
+static gboolean
+tmpl_branch_node_accept (TmplNode *node,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplBranchNode *self = (TmplBranchNode *)node;
+
+ TMPL_ENTRY;
+
+ g_assert (TMPL_IS_BRANCH_NODE (self));
+ g_assert (self->if_branch != NULL);
+ g_assert (lexer != NULL);
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ if (!tmpl_node_accept (self->if_branch, lexer, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ /*
+ * At this point, the if branch should have everything, so we are
+ * looking for ELSE_IF, ELSE, or END. Everything else is a syntax
+ * error.
+ */
+
+ while (TRUE)
+ {
+ TmplToken *token = NULL;
+
+ if (!tmpl_lexer_next (lexer, &token, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ switch (tmpl_token_type (token))
+ {
+ case TMPL_TOKEN_EOF:
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Unexpected end-of-file reached");
+ TMPL_RETURN (FALSE);
+
+ case TMPL_TOKEN_END:
+ tmpl_token_free (token);
+ TMPL_RETURN (TRUE);
+
+ case TMPL_TOKEN_ELSE:
+ case TMPL_TOKEN_ELSE_IF:
+ {
+ TmplNode *child;
+ TmplExpr *expr = NULL;
+
+ if (tmpl_token_type (token) != TMPL_TOKEN_ELSE)
+ {
+ const gchar *exprstr;
+
+ exprstr = tmpl_token_get_text (token);
+ expr = tmpl_expr_from_string (exprstr, error);
+ }
+ else
+ expr = tmpl_expr_new_boolean (TRUE);
+
+ tmpl_token_free (token);
+
+ if (expr == NULL)
+ TMPL_RETURN (FALSE);
+
+ child = tmpl_condition_node_new (expr);
+
+ if (self->children == NULL)
+ self->children = g_ptr_array_new_with_free_func (g_object_unref);
+ g_ptr_array_add (self->children, child);
+
+ if (!tmpl_node_accept (child, lexer, cancellable, error))
+ TMPL_RETURN (FALSE);
+ }
+ break;
+
+ case TMPL_TOKEN_TEXT:
+ case TMPL_TOKEN_IF:
+ case TMPL_TOKEN_EXPRESSION:
+ case TMPL_TOKEN_FOR:
+ case TMPL_TOKEN_INCLUDE:
+ default:
+ tmpl_token_free (token);
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Invalid token, expected else if, else, or end.");
+ TMPL_RETURN (FALSE);
+ }
+ }
+}
+
+static void
+tmpl_branch_node_visit_children (TmplNode *node,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ TmplBranchNode *self = (TmplBranchNode *)node;
+
+ g_assert (TMPL_IS_NODE (node));
+ g_assert (visitor != NULL);
+
+ if (self->if_branch)
+ visitor (self->if_branch, user_data);
+
+ if (self->children != NULL)
+ {
+ gint i;
+
+ for (i = 0; i < self->children->len; i++)
+ {
+ TmplNode *child = g_ptr_array_index (self->children, i);
+
+ visitor (child, user_data);
+ }
+ }
+}
+
+static void
+tmpl_branch_node_finalize (GObject *object)
+{
+ TmplBranchNode *self = (TmplBranchNode *)object;
+
+ g_clear_pointer (&self->children, g_ptr_array_unref);
+ g_clear_object (&self->if_branch);
+
+ G_OBJECT_CLASS (tmpl_branch_node_parent_class)->finalize (object);
+}
+
+static void
+tmpl_branch_node_class_init (TmplBranchNodeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ TmplNodeClass *node_class = TMPL_NODE_CLASS (klass);
+
+ object_class->finalize = tmpl_branch_node_finalize;
+
+ node_class->accept = tmpl_branch_node_accept;
+ node_class->visit_children = tmpl_branch_node_visit_children;
+}
+
+static void
+tmpl_branch_node_init (TmplBranchNode *self)
+{
+}
+
+/**
+ * tmpl_branch_node_new:
+ * @expr: (transfer full): A TmplExpr
+ *
+ * Creates a new branch (if, else if, else) using @expr for the (if)
+ * branch.
+ *
+ * Returns: (transfer full): A #TmplBranchNode.
+ */
+TmplNode *
+tmpl_branch_node_new (TmplExpr *condition)
+{
+ TmplBranchNode *self;
+
+ self = g_object_new (TMPL_TYPE_BRANCH_NODE, NULL);
+ self->if_branch = tmpl_condition_node_new (condition);
+
+ return TMPL_NODE (self);
+}
+
+static gboolean
+condition_matches (TmplNode *condition,
+ TmplScope *scope,
+ GError **error)
+{
+ TmplExpr *expr;
+ GValue value = G_VALUE_INIT;
+ gboolean ret;
+
+ g_assert (TMPL_IS_CONDITION_NODE (condition));
+
+ if (!(expr = tmpl_condition_node_get_condition (TMPL_CONDITION_NODE (condition))))
+ return FALSE;
+
+ if (!tmpl_expr_eval (expr, scope, &value, error))
+ return FALSE;
+
+ ret = tmpl_value_as_boolean (&value);
+ TMPL_CLEAR_VALUE (&value);
+ return ret;
+}
+
+TmplNode *
+tmpl_branch_node_branch (TmplBranchNode *self,
+ TmplScope *scope,
+ GError **error)
+{
+ GError *local_error = NULL;
+
+ TMPL_ENTRY;
+
+ g_return_val_if_fail (TMPL_IS_BRANCH_NODE (self), NULL);
+ g_return_val_if_fail (self->if_branch != NULL, NULL);
+
+ if (condition_matches (self->if_branch, scope, &local_error))
+ return self->if_branch;
+
+ if (local_error != NULL)
+ {
+ g_propagate_error (error, local_error);
+ TMPL_RETURN (NULL);
+ }
+
+ if (self->children != NULL)
+ {
+ gint i;
+
+ for (i = 0; i < self->children->len; i++)
+ {
+ TmplNode *child = g_ptr_array_index (self->children, i);
+
+ if (condition_matches (child, scope, &local_error))
+ TMPL_RETURN (child);
+
+ if (local_error != NULL)
+ {
+ g_propagate_error (error, local_error);
+ TMPL_RETURN (NULL);
+ }
+ }
+ }
+
+ TMPL_RETURN (NULL);
+}
diff --git a/contrib/tmpl/tmpl-branch-node.h b/contrib/tmpl/tmpl-branch-node.h
new file mode 100644
index 0000000..2add310
--- /dev/null
+++ b/contrib/tmpl/tmpl-branch-node.h
@@ -0,0 +1,43 @@
+/* tmpl-branch-node.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_BRANCH_NODE_H
+#define TMPL_BRANCH_NODE_H
+
+#include "tmpl-expr.h"
+#include "tmpl-node.h"
+#include "tmpl-scope.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_BRANCH_NODE (tmpl_branch_node_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplBranchNode, tmpl_branch_node, TMPL, BRANCH_NODE, TmplNode)
+
+TmplNode *tmpl_branch_node_new (TmplExpr *condition);
+TmplNode *tmpl_branch_node_branch (TmplBranchNode *self,
+ TmplScope *scope,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* TMPL_BRANCH_NODE_H */
diff --git a/contrib/tmpl/tmpl-condition-node.c b/contrib/tmpl/tmpl-condition-node.c
new file mode 100644
index 0000000..9f7ef21
--- /dev/null
+++ b/contrib/tmpl/tmpl-condition-node.c
@@ -0,0 +1,178 @@
+/* tmpl-condition-node.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-condition-node"
+
+#include "tmpl-condition-node.h"
+#include "tmpl-debug.h"
+#include "tmpl-error.h"
+
+struct _TmplConditionNode
+{
+ TmplNode parent_instance;
+
+ GPtrArray *children;
+ TmplExpr *condition;
+};
+
+G_DEFINE_TYPE (TmplConditionNode, tmpl_condition_node, TMPL_TYPE_NODE)
+
+static gboolean
+tmpl_condition_node_accept (TmplNode *node,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplConditionNode *self = (TmplConditionNode *)node;
+ TmplToken *token = NULL;
+
+ TMPL_ENTRY;
+
+ g_assert (TMPL_IS_NODE (node));
+ g_assert (lexer != NULL);
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ /*
+ * We are an if/else if/else condition, if we come across an
+ * end/else if/else then we need to unget and allow the parent
+ * branch to resolve.
+ */
+
+ while (TRUE)
+ {
+ TmplNode *child;
+
+ if (!tmpl_lexer_next (lexer, &token, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ switch (tmpl_token_type (token))
+ {
+ case TMPL_TOKEN_EOF:
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Unexpected end-of-file reached.");
+ TMPL_RETURN (FALSE);
+
+ case TMPL_TOKEN_ELSE_IF:
+ case TMPL_TOKEN_ELSE:
+ case TMPL_TOKEN_END:
+ tmpl_lexer_unget (lexer, token);
+ TMPL_RETURN (TRUE);
+
+ case TMPL_TOKEN_TEXT:
+ case TMPL_TOKEN_IF:
+ case TMPL_TOKEN_FOR:
+ case TMPL_TOKEN_EXPRESSION:
+ child = tmpl_node_new_for_token (token, error);
+ tmpl_token_free (token);
+
+ if (child == NULL)
+ TMPL_RETURN (FALSE);
+
+ if (self->children == NULL)
+ self->children = g_ptr_array_new_with_free_func (g_object_unref);
+
+ g_ptr_array_add (self->children, child);
+
+ if (!tmpl_node_accept (child, lexer, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ break;
+
+ case TMPL_TOKEN_INCLUDE:
+ default:
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Invalid token type");
+ TMPL_RETURN (FALSE);
+ }
+ }
+
+ g_assert_not_reached ();
+}
+
+static void
+tmpl_condition_node_visit_children (TmplNode *node,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ TmplConditionNode *self = (TmplConditionNode *)node;
+
+ g_assert (TMPL_IS_CONDITION_NODE (self));
+ g_assert (visitor != NULL);
+
+ if (self->children != NULL)
+ {
+ gint i;
+
+ for (i = 0; i < self->children->len; i++)
+ {
+ TmplNode *child = g_ptr_array_index (self->children, i);
+ visitor (child, user_data);
+ }
+ }
+}
+
+static void
+tmpl_condition_node_finalize (GObject *object)
+{
+ TmplConditionNode *self = (TmplConditionNode *)object;
+
+ g_clear_pointer (&self->condition, tmpl_expr_unref);
+ g_clear_pointer (&self->children, g_ptr_array_unref);
+
+ G_OBJECT_CLASS (tmpl_condition_node_parent_class)->finalize (object);
+}
+
+static void
+tmpl_condition_node_class_init (TmplConditionNodeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ TmplNodeClass *node_class = TMPL_NODE_CLASS (klass);
+
+ object_class->finalize = tmpl_condition_node_finalize;
+
+ node_class->accept = tmpl_condition_node_accept;
+ node_class->visit_children = tmpl_condition_node_visit_children;
+}
+
+static void
+tmpl_condition_node_init (TmplConditionNode *self)
+{
+}
+
+TmplNode *
+tmpl_condition_node_new (TmplExpr *condition)
+{
+ TmplConditionNode *self;
+
+ self = g_object_new (TMPL_TYPE_CONDITION_NODE, NULL);
+ self->condition = condition;
+
+ return TMPL_NODE (self);
+}
+
+TmplExpr *
+tmpl_condition_node_get_condition (TmplConditionNode *self)
+{
+ g_return_val_if_fail (TMPL_IS_CONDITION_NODE (self), NULL);
+
+ return self->condition;
+}
diff --git a/contrib/tmpl/tmpl-condition-node.h b/contrib/tmpl/tmpl-condition-node.h
new file mode 100644
index 0000000..be4b9a0
--- /dev/null
+++ b/contrib/tmpl/tmpl-condition-node.h
@@ -0,0 +1,40 @@
+/* tmpl-condition-node.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_CONDITION_NODE_H
+#define TMPL_CONDITION_NODE_H
+
+#include "tmpl-expr.h"
+#include "tmpl-node.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_CONDITION_NODE (tmpl_condition_node_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplConditionNode, tmpl_condition_node, TMPL, CONDITION_NODE, TmplNode)
+
+TmplNode *tmpl_condition_node_new (TmplExpr *expr);
+TmplExpr *tmpl_condition_node_get_condition (TmplConditionNode *self);
+
+G_END_DECLS
+
+#endif /* TMPL_CONDITION_NODE_H */
diff --git a/contrib/tmpl/tmpl-debug.h.in b/contrib/tmpl/tmpl-debug.h.in
new file mode 100644
index 0000000..04cf975
--- /dev/null
+++ b/contrib/tmpl/tmpl-debug.h.in
@@ -0,0 +1,80 @@
+/* ide-debug.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_DEBUG_H
+#define TMPL_DEBUG_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#ifndef TMPL_ENABLE_TRACE
+# define TMPL_ENABLE_TRACE @ENABLE_TRACING@
+#endif
+#if TMPL_ENABLE_TRACE != 1
+# undef TMPL_ENABLE_TRACE
+#endif
+
+#ifndef G_LOG_LEVEL_TRACE
+# define G_LOG_LEVEL_TRACE (1 << G_LOG_LEVEL_USER_SHIFT)
+#endif
+
+#ifdef TMPL_ENABLE_TRACE
+# define TMPL_TRACE_MSG(fmt, ...) \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, " MSG: %s():%d: "fmt, \
+ G_STRFUNC, __LINE__, ##__VA_ARGS__)
+# define TMPL_PROBE \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, "PROBE: %s():%d", \
+ G_STRFUNC, __LINE__)
+# define TMPL_TODO(_msg) \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, " TODO: %s():%d: %s", \
+ G_STRFUNC, __LINE__, _msg)
+# define TMPL_ENTRY \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, "ENTRY: %s():%d", \
+ G_STRFUNC, __LINE__)
+# define TMPL_EXIT \
+ G_STMT_START { \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, " EXIT: %s():%d", \
+ G_STRFUNC, __LINE__); \
+ return; \
+ } G_STMT_END
+# define TMPL_GOTO(_l) \
+ G_STMT_START { \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, " GOTO: %s():%d ("#_l")", \
+ G_STRFUNC, __LINE__); \
+ goto _l; \
+ } G_STMT_END
+# define TMPL_RETURN(_r) \
+ G_STMT_START { \
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_TRACE, " EXIT: %s():%d ", \
+ G_STRFUNC, __LINE__); \
+ return _r; \
+ } G_STMT_END
+#else
+# define TMPL_TODO(_msg)
+# define TMPL_PROBE
+# define TMPL_TRACE_MSG(fmt, ...)
+# define TMPL_ENTRY
+# define TMPL_GOTO(_l) goto _l
+# define TMPL_EXIT return
+# define TMPL_RETURN(_r) return _r
+#endif
+
+G_END_DECLS
+
+#endif /* TMPL_DEBUG_H */
diff --git a/contrib/tmpl/tmpl-enums.c.in b/contrib/tmpl/tmpl-enums.c.in
new file mode 100644
index 0000000..e94c599
--- /dev/null
+++ b/contrib/tmpl/tmpl-enums.c.in
@@ -0,0 +1,42 @@
+/*** BEGIN file-header ***/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "tmpl-enums.h"
+
+#include "tmpl-error.h"
+#include "tmpl-expr-types.h"
+
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+/* enumerations from "@filename@" */
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType
+ enum_name@_get_type (void)
+{
+ static GType etype = 0;
+ if (G_UNLIKELY(etype == 0)) {
+ static const G Type@Value values[] = {
+/*** END value-header ***/
+
+/*** BEGIN value-production ***/
+ { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
+/*** END value-production ***/
+
+/*** BEGIN value-tail ***/
+ { 0, NULL, NULL }
+ };
+ etype = g_ type@_register_static (g_intern_static_string ("@EnumName@"), values);
+ }
+ return etype;
+}
+
+/*** END value-tail ***/
+
+/*** BEGIN file-tail ***/
+
+/*** END file-tail ***/
diff --git a/contrib/tmpl/tmpl-enums.h.in b/contrib/tmpl/tmpl-enums.h.in
new file mode 100644
index 0000000..3722cdc
--- /dev/null
+++ b/contrib/tmpl/tmpl-enums.h.in
@@ -0,0 +1,24 @@
+/*** BEGIN file-header ***/
+#ifndef __IDE_ENUMS_H__
+#define __IDE_ENUMS_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+
+/* enumerations from "@filename@" */
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType @enum_name _get_type (void);
+#define @ENUMPREFIX _TYPE_@ENUMSHORT@ (@enum_name _get_type ())
+/*** END value-header ***/
+
+/*** BEGIN file-tail ***/
+G_END_DECLS
+
+#endif /* __IDE_ENUMS_H__ */
+/*** END file-tail ***/
diff --git a/contrib/tmpl/tmpl-error.c b/contrib/tmpl/tmpl-error.c
new file mode 100644
index 0000000..3306183
--- /dev/null
+++ b/contrib/tmpl/tmpl-error.c
@@ -0,0 +1,25 @@
+/* tmpl-error.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tmpl-error.h"
+
+GQuark
+tmpl_error_quark (void)
+{
+ return g_quark_from_static_string ("tmpl-error");
+}
diff --git a/contrib/tmpl/tmpl-error.h b/contrib/tmpl/tmpl-error.h
new file mode 100644
index 0000000..b475293
--- /dev/null
+++ b/contrib/tmpl/tmpl-error.h
@@ -0,0 +1,58 @@
+/* gis-error.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_ERROR_H
+#define TMPL_ERROR_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define TMPL_ERROR (tmpl_error_quark())
+
+typedef enum
+{
+ TMPL_ERROR_INVALID_STATE = 1,
+ TMPL_ERROR_TEMPLATE_NOT_FOUND,
+ TMPL_ERROR_CIRCULAR_INCLUDE,
+ TMPL_ERROR_SYNTAX_ERROR,
+ TMPL_ERROR_LEXER_FAILURE,
+ TMPL_ERROR_TYPE_MISMATCH,
+ TMPL_ERROR_INVALID_OP_CODE,
+ TMPL_ERROR_DIVIDE_BY_ZERO,
+ TMPL_ERROR_MISSING_SYMBOL,
+ TMPL_ERROR_SYMBOL_REDEFINED,
+ TMPL_ERROR_NOT_AN_OBJECT,
+ TMPL_ERROR_NULL_POINTER,
+ TMPL_ERROR_NO_SUCH_PROPERTY,
+ TMPL_ERROR_GI_FAILURE,
+ TMPL_ERROR_RUNTIME_ERROR,
+ TMPL_ERROR_NOT_IMPLEMENTED,
+ TMPL_ERROR_NOT_A_VALUE,
+ TMPL_ERROR_NOT_A_FUNCTION,
+} TmplError;
+
+GQuark tmpl_error_quark (void);
+
+G_END_DECLS
+
+#endif /* TMPL_ERROR_H */
diff --git a/contrib/tmpl/tmpl-expr-eval.c b/contrib/tmpl/tmpl-expr-eval.c
new file mode 100644
index 0000000..bfa05ea
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-eval.c
@@ -0,0 +1,1153 @@
+/* tmpl-expr-eval.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <girepository.h>
+#include <math.h>
+
+#include "tmpl-error.h"
+#include "tmpl-expr.h"
+#include "tmpl-expr-private.h"
+#include "tmpl-gi-private.h"
+#include "tmpl-scope.h"
+#include "tmpl-symbol.h"
+#include "tmpl-util-private.h"
+
+typedef gboolean (*BuiltinFunc) (const GValue *value,
+ GValue *return_value,
+ GError **error);
+typedef gboolean (*FastDispatch) (const GValue *left,
+ const GValue *right,
+ GValue *return_value,
+ GError **error);
+
+static gboolean tmpl_expr_eval_internal (TmplExpr *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error);
+static gboolean throw_type_mismatch (GError **error,
+ const GValue *left,
+ const GValue *right,
+ const gchar *message);
+static gboolean builtin_abs (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_ceil (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_floor (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_hex (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_log (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_print (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_repr (const GValue *value,
+ GValue *return_value,
+ GError **error);
+static gboolean builtin_sqrt (const GValue *value,
+ GValue *return_value,
+ GError **error);
+
+static GHashTable *fast_dispatch;
+static BuiltinFunc builtin_funcs [] = {
+ builtin_abs,
+ builtin_ceil,
+ builtin_floor,
+ builtin_hex,
+ builtin_log,
+ builtin_print,
+ builtin_repr,
+ builtin_sqrt,
+};
+
+static inline guint
+build_hash (TmplExprType type,
+ GType left,
+ GType right)
+{
+ g_assert (!left || G_TYPE_IS_FUNDAMENTAL (left));
+ g_assert (!right || G_TYPE_IS_FUNDAMENTAL (right));
+
+ return type | (left << 16) | (right << 24);
+}
+
+
+static gboolean
+throw_type_mismatch (GError **error,
+ const GValue *left,
+ const GValue *right,
+ const gchar *message)
+{
+ if (right != NULL)
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_TYPE_MISMATCH,
+ "%s: %s and %s",
+ message,
+ G_VALUE_TYPE_NAME (left),
+ G_VALUE_TYPE_NAME (right));
+ else
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_TYPE_MISMATCH,
+ "%s: %s", message, G_VALUE_TYPE_NAME (left));
+
+ return TRUE;
+}
+
+#define SIMPLE_NUMBER_OP(op, left, right, return_value, error) \
+ G_STMT_START { \
+ if (G_VALUE_HOLDS (left, G_VALUE_TYPE (right))) \
+ { \
+ if (G_VALUE_HOLDS (left, G_TYPE_DOUBLE)) \
+ { \
+ g_value_init (return_value, G_TYPE_DOUBLE); \
+ g_value_set_double (return_value, \
+ g_value_get_double (left) \
+ op \
+ g_value_get_double (right)); \
+ return TRUE; \
+ } \
+ } \
+ return throw_type_mismatch (error, left, right, "invalid add"); \
+ } G_STMT_END
+
+static gboolean
+tmpl_expr_simple_eval (TmplExprSimple *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue left = G_VALUE_INIT;
+ GValue right = G_VALUE_INIT;
+ gboolean ret = FALSE;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (tmpl_expr_eval_internal (node->left, scope, &left, error) &&
+ ((node->right == NULL) ||
+ tmpl_expr_eval_internal (node->right, scope, &right, error)))
+ {
+ FastDispatch dispatch;
+ guint hash;
+
+ hash = build_hash (node->type, G_VALUE_TYPE (&left), G_VALUE_TYPE (&right));
+ dispatch = g_hash_table_lookup (fast_dispatch, GINT_TO_POINTER (hash));
+
+ if (G_UNLIKELY (dispatch == NULL))
+ {
+ throw_type_mismatch (error, &left, &right, "type mismatch");
+ goto cleanup;
+ }
+
+ ret = dispatch (&left, &right, return_value, error);
+ }
+
+cleanup:
+ TMPL_CLEAR_VALUE (&left);
+ TMPL_CLEAR_VALUE (&right);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_fn_call_eval (TmplExprFnCall *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue left = G_VALUE_INIT;
+ gboolean ret = FALSE;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (tmpl_expr_eval_internal (node->param, scope, &left, error))
+ ret = builtin_funcs [node->builtin] (&left, return_value, error);
+
+ TMPL_CLEAR_VALUE (&left);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_flow_eval (TmplExprFlow *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue cond = G_VALUE_INIT;
+ gboolean ret = FALSE;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (!tmpl_expr_eval_internal (node->condition, scope, &cond, error))
+ goto cleanup;
+
+ if (node->type == TMPL_EXPR_IF)
+ {
+ if (tmpl_value_as_boolean (&cond))
+ {
+ if (node->primary != NULL)
+ {
+ ret = tmpl_expr_eval_internal (node->primary, scope, return_value, error);
+ goto cleanup;
+ }
+ }
+ else
+ {
+ if (node->secondary != NULL)
+ {
+ ret = tmpl_expr_eval_internal (node->secondary, scope, return_value, error);
+ goto cleanup;
+ }
+ }
+ }
+ else if (node->type == TMPL_EXPR_WHILE)
+ {
+ if (node->primary != NULL)
+ {
+ while (tmpl_value_as_boolean (&cond))
+ {
+ /* last iteration is result value */
+ g_value_unset (return_value);
+ if (!tmpl_expr_eval_internal (node->primary, scope, return_value, error))
+ goto cleanup;
+
+ g_value_unset (&cond);
+ if (!tmpl_expr_eval_internal (node->condition, scope, &cond, error))
+ goto cleanup;
+ }
+ }
+ }
+
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_INVALID_STATE,
+ "Invalid AST");
+
+cleanup:
+ TMPL_CLEAR_VALUE (&cond);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_stmt_list_eval (TmplExprSimple *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue left = G_VALUE_INIT;
+ gboolean ret = FALSE;
+
+ if (!tmpl_expr_eval_internal (node->left, scope, &left, error))
+ goto cleanup;
+
+ if (!tmpl_expr_eval_internal (node->left, scope, return_value, error))
+ goto cleanup;
+
+ ret = TRUE;
+
+cleanup:
+ TMPL_CLEAR_VALUE (&left);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_symbol_ref_eval (TmplExprSymbolRef *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ TmplSymbol *symbol;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+
+ symbol = tmpl_scope_peek (scope, node->symbol);
+
+ if (symbol == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_MISSING_SYMBOL,
+ "No such symbol \"%s\" in scope",
+ node->symbol);
+ return FALSE;
+ }
+
+ if (tmpl_symbol_get_symbol_type (symbol) == TMPL_SYMBOL_VALUE)
+ {
+ tmpl_symbol_get_value (symbol, return_value);
+ return TRUE;
+ }
+
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_A_VALUE,
+ "The symbol \"%s\" is not a value",
+ node->symbol);
+
+ return FALSE;
+}
+
+static gboolean
+tmpl_expr_symbol_assign_eval (TmplExprSymbolAssign *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ TmplSymbol *symbol;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (!tmpl_expr_eval_internal (node->right, scope, return_value, error))
+ return FALSE;
+
+ symbol = tmpl_scope_get (scope, node->symbol);
+ tmpl_symbol_assign_value (symbol, return_value);
+
+ return TRUE;
+}
+
+static gboolean
+tmpl_expr_getattr_eval (TmplExprGetattr *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue left = G_VALUE_INIT;
+ GParamSpec *pspec;
+ GObject *object;
+ gboolean ret = FALSE;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (!tmpl_expr_eval_internal (node->left, scope, &left, error))
+ goto cleanup;
+
+ if (!G_VALUE_HOLDS_OBJECT (&left))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_AN_OBJECT,
+ "Cannot access property \"%s\" of non-object \"%s\"",
+ node->attr, G_VALUE_TYPE_NAME (&left));
+ goto cleanup;
+ }
+
+ object = g_value_get_object (&left);
+
+ if (object == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NULL_POINTER,
+ "Cannot access property of null object");
+ goto cleanup;
+ }
+
+ if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), node->attr)))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NO_SUCH_PROPERTY,
+ "No such property \"%s\" on object \"%s\"",
+ node->attr, G_OBJECT_TYPE_NAME (object));
+ goto cleanup;
+ }
+
+ g_value_init (return_value, pspec->value_type);
+ g_object_get_property (object, node->attr, return_value);
+
+ ret = TRUE;
+
+cleanup:
+ TMPL_CLEAR_VALUE (&left);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_setattr_eval (TmplExprSetattr *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue left = G_VALUE_INIT;
+ GValue right = G_VALUE_INIT;
+ GObject *object;
+ gboolean ret = FALSE;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (!tmpl_expr_eval_internal (node->left, scope, &left, error))
+ goto cleanup;
+
+ if (!G_VALUE_HOLDS_OBJECT (&left))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_AN_OBJECT,
+ "Cannot access property \"%s\" of non-object \"%s\"",
+ node->attr, G_VALUE_TYPE_NAME (&left));
+ goto cleanup;
+ }
+
+ object = g_value_get_object (&left);
+
+ if (object == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NULL_POINTER,
+ "Cannot access property of null object");
+ goto cleanup;
+ }
+
+ if (!g_object_class_find_property (G_OBJECT_GET_CLASS (object), node->attr))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NO_SUCH_PROPERTY,
+ "No such property \"%s\" on object \"%s\"",
+ node->attr, G_OBJECT_TYPE_NAME (object));
+ goto cleanup;
+ }
+
+ if (!tmpl_expr_eval_internal (node->right, scope, &right, error))
+ goto cleanup;
+
+ g_object_set_property (object, node->attr, &right);
+
+ g_value_init (return_value, G_VALUE_TYPE (&right));
+ g_value_copy (&right, return_value);
+
+ ret = TRUE;
+
+cleanup:
+ TMPL_CLEAR_VALUE (&left);
+ TMPL_CLEAR_VALUE (&right);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_gi_call_eval (TmplExprGiCall *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GValue left = G_VALUE_INIT;
+ GValue right = G_VALUE_INIT;
+ GIRepository *repository;
+ GIBaseInfo *base_info;
+ GIFunctionInfo *function = NULL;
+ GIArgument return_value_arg = { 0 };
+ GITypeInfo return_value_type;
+ TmplExpr *args;
+ GObject *object;
+ gboolean ret = FALSE;
+ GArray *in_args = NULL;
+ GArray *values = NULL;
+ GType type;
+ guint n_args;
+ guint i;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ if (!tmpl_expr_eval_internal (node->object, scope, &left, error))
+ goto cleanup;
+
+ if (!G_VALUE_HOLDS_OBJECT (&left))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_AN_OBJECT,
+ "Cannot access function \"%s\" of non-object \"%s\"",
+ node->name, G_VALUE_TYPE_NAME (&left));
+ goto cleanup;
+ }
+
+ object = g_value_get_object (&left);
+
+ if (object == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NULL_POINTER,
+ "Cannot access function of null object");
+ goto cleanup;
+ }
+
+ repository = g_irepository_get_default ();
+ type = G_OBJECT_TYPE (object);
+
+ while (g_type_is_a (type, G_TYPE_OBJECT))
+ {
+ base_info = g_irepository_find_by_gtype (repository, type);
+
+ if (base_info == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_GI_FAILURE,
+ "Failed to locate GObject Introspection data. "
+ "Consider importing required module.");
+ goto cleanup;
+ }
+
+ function = g_object_info_find_method ((GIObjectInfo *)base_info, node->name);
+ if (function != NULL)
+ break;
+
+ type = g_type_parent (type);
+ }
+
+ if (function == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_GI_FAILURE,
+ "No such method \"%s\" on object \"%s\"",
+ node->name, G_OBJECT_TYPE_NAME (object));
+ goto cleanup;
+ }
+
+ n_args = g_callable_info_get_n_args ((GICallableInfo *)function);
+
+ values = g_array_new (FALSE, TRUE, sizeof (GValue));
+ g_array_set_size (values, n_args);
+
+ in_args = g_array_new (FALSE, TRUE, sizeof (GIArgument));
+ g_array_set_size (in_args, n_args + 1);
+
+ g_array_index (in_args, GIArgument, 0).v_pointer = object;
+
+ args = node->params;
+
+ for (i = 0; i < n_args; i++)
+ {
+ GIArgInfo *arg_info = g_callable_info_get_arg ((GICallableInfo *)function, i);
+ GIArgument *arg = &g_array_index (in_args, GIArgument, i + 1);
+ GValue *value = &g_array_index (values, GValue, i);
+ GITypeInfo type_info = { 0 };
+
+ if (g_arg_info_get_direction (arg_info) != GI_DIRECTION_IN)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_RUNTIME_ERROR,
+ "Only \"in\" parameters are supported");
+ goto cleanup;
+ }
+
+ if (args == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Too few arguments to function \"%s\"",
+ node->name);
+ goto cleanup;
+ }
+
+ if (args->any.type == TMPL_EXPR_STMT_LIST)
+ {
+ if (!tmpl_expr_eval_internal (((TmplExprSimple *)node)->left, scope, value, error))
+ goto cleanup;
+
+ args = ((TmplExprSimple *)args)->right;
+ }
+ else
+ {
+ if (!tmpl_expr_eval_internal (args, scope, value, error))
+ goto cleanup;
+
+ args = NULL;
+ }
+
+ g_arg_info_load_type (arg_info, &type_info);
+
+ if (!tmpl_gi_argument_from_g_value (value, &type_info, arg, error))
+ goto cleanup;
+ }
+
+ if ((args != NULL) && (n_args > 0))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Too many arguments to function \"%s\"",
+ node->name);
+ goto cleanup;
+ }
+
+ if (!g_function_info_invoke (function,
+ (GIArgument *)(void *)in_args->data,
+ in_args->len,
+ NULL,
+ 0,
+ &return_value_arg,
+ error))
+ goto cleanup;
+
+ g_callable_info_load_return_type ((GICallableInfo *)function, &return_value_type);
+
+ if (!tmpl_gi_argument_to_g_value (return_value, &return_value_type, &return_value_arg, error))
+ goto cleanup;
+
+ ret = TRUE;
+
+cleanup:
+ g_clear_pointer (&in_args, g_array_unref);
+
+ if (values != NULL)
+ {
+ for (i = 0; i < values->len; i++)
+ {
+ GValue *value = &g_array_index (values, GValue, i);
+
+ if (G_VALUE_TYPE (value) != G_TYPE_INVALID)
+ g_value_unset (value);
+ }
+
+ g_clear_pointer (&values, g_array_unref);
+ }
+
+ TMPL_CLEAR_VALUE (&left);
+ TMPL_CLEAR_VALUE (&right);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_user_fn_call_eval (TmplExprUserFnCall *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GPtrArray *args = NULL;
+ TmplExpr *expr = NULL;
+ TmplExpr *params = NULL;
+ TmplScope *local_scope = NULL;
+ TmplSymbol *symbol;
+ gboolean ret = FALSE;
+ gint n_args = 0;
+ gint i;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ symbol = tmpl_scope_peek (scope, node->symbol);
+
+ if (symbol == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_MISSING_SYMBOL,
+ "No such function \"%s\"",
+ node->symbol);
+ return FALSE;
+ }
+
+ if (tmpl_symbol_get_symbol_type (symbol) != TMPL_SYMBOL_EXPR)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_A_FUNCTION,
+ "\"%s\" is not a function",
+ node->symbol);
+ return FALSE;
+ }
+
+ expr = tmpl_symbol_get_expr (symbol, &args);
+ n_args = args != NULL ? args->len : 0;
+
+ local_scope = tmpl_scope_new_with_parent (scope);
+
+ params = node->params;
+
+ for (i = 0; i < n_args; i++)
+ {
+ const gchar *arg = g_ptr_array_index (args, i);
+ GValue value = G_VALUE_INIT;
+
+ if (params == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "\"%s\" takes %d arguments, not %d",
+ node->symbol, n_args, i);
+ return FALSE;
+ }
+
+ if (params->any.type == TMPL_EXPR_STMT_LIST)
+ {
+ TmplExprSimple *simple = (TmplExprSimple *)params;
+
+ if (!tmpl_expr_eval_internal (simple->left, local_scope, &value, error))
+ goto cleanup;
+
+ params = simple->right;
+ }
+ else
+ {
+ if (!tmpl_expr_eval_internal (params, local_scope, &value, error))
+ goto cleanup;
+
+ params = NULL;
+ }
+
+ symbol = tmpl_scope_get (local_scope, arg);
+ tmpl_symbol_assign_value (symbol, &value);
+
+ TMPL_CLEAR_VALUE (&value);
+ }
+
+ if (params != NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "\"%s\" takes %d params",
+ node->symbol, n_args);
+ goto cleanup;
+ }
+
+ if (!tmpl_expr_eval_internal (expr, local_scope, return_value, error))
+ goto cleanup;
+
+ ret = TRUE;
+
+cleanup:
+ g_clear_pointer (&local_scope, tmpl_scope_unref);
+
+ return ret;
+}
+
+static gboolean
+tmpl_expr_require_eval (TmplExprRequire *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ GITypelib *typelib;
+ TmplSymbol *symbol;
+
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ typelib = g_irepository_require (g_irepository_get_default (),
+ node->name,
+ node->version,
+ G_IREPOSITORY_LOAD_FLAG_LAZY,
+ error);
+
+ if (typelib == NULL)
+ return FALSE;
+
+ g_value_init (return_value, TMPL_TYPE_TYPELIB);
+ g_value_set_pointer (return_value, typelib);
+
+ symbol = tmpl_scope_get (scope, node->name);
+ tmpl_symbol_assign_value (symbol, return_value);
+
+ return TRUE;
+}
+
+static gboolean
+tmpl_expr_eval_internal (TmplExpr *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ g_assert (node != NULL);
+ g_assert (scope != NULL);
+ g_assert (return_value != NULL);
+
+ switch (node->any.type)
+ {
+ case TMPL_EXPR_ADD:
+ case TMPL_EXPR_SUB:
+ case TMPL_EXPR_MUL:
+ case TMPL_EXPR_DIV:
+ case TMPL_EXPR_UNARY_MINUS:
+ case TMPL_EXPR_GT:
+ case TMPL_EXPR_LT:
+ case TMPL_EXPR_NE:
+ case TMPL_EXPR_EQ:
+ case TMPL_EXPR_GTE:
+ case TMPL_EXPR_LTE:
+ return tmpl_expr_simple_eval ((TmplExprSimple *)node, scope, return_value, error);
+
+ case TMPL_EXPR_NUMBER:
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, ((TmplExprNumber *)node)->number);
+ return TRUE;
+
+ case TMPL_EXPR_BOOLEAN:
+ g_value_init (return_value, G_TYPE_BOOLEAN);
+ g_value_set_boolean (return_value, ((TmplExprBoolean *)node)->value);
+ return TRUE;
+
+ case TMPL_EXPR_STRING:
+ g_value_init (return_value, G_TYPE_STRING);
+ g_value_set_string (return_value, ((TmplExprString *)node)->value);
+ return TRUE;
+
+ case TMPL_EXPR_STMT_LIST:
+ tmpl_expr_stmt_list_eval ((TmplExprSimple *)node, scope, return_value, error);
+ break;
+
+ case TMPL_EXPR_IF:
+ case TMPL_EXPR_WHILE:
+ return tmpl_expr_flow_eval ((TmplExprFlow *)node, scope, return_value, error);
+
+ case TMPL_EXPR_SYMBOL_REF:
+ return tmpl_expr_symbol_ref_eval ((TmplExprSymbolRef *)node, scope, return_value, error);
+
+ case TMPL_EXPR_SYMBOL_ASSIGN:
+ return tmpl_expr_symbol_assign_eval ((TmplExprSymbolAssign *)node, scope, return_value, error);
+ break;
+
+ case TMPL_EXPR_FN_CALL:
+ return tmpl_expr_fn_call_eval ((TmplExprFnCall *)node, scope, return_value, error);
+
+ case TMPL_EXPR_USER_FN_CALL:
+ return tmpl_expr_user_fn_call_eval ((TmplExprUserFnCall *)node, scope, return_value, error);
+
+ case TMPL_EXPR_GI_CALL:
+ return tmpl_expr_gi_call_eval ((TmplExprGiCall *)node, scope, return_value, error);
+
+ case TMPL_EXPR_GETATTR:
+ return tmpl_expr_getattr_eval ((TmplExprGetattr *)node, scope, return_value, error);
+
+ case TMPL_EXPR_SETATTR:
+ return tmpl_expr_setattr_eval ((TmplExprSetattr *)node, scope, return_value, error);
+
+ case TMPL_EXPR_REQUIRE:
+ return tmpl_expr_require_eval ((TmplExprRequire *)node, scope, return_value, error);
+
+ default:
+ break;
+ }
+
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_INVALID_OP_CODE,
+ "invalid opcode: %04x", node->any.type);
+
+ return FALSE;
+}
+
+static gboolean
+div_double_double (const GValue *left,
+ const GValue *right,
+ GValue *return_value,
+ GError **error)
+{
+ gdouble denom = g_value_get_double (right);
+
+ if (denom == 0.0)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_DIVIDE_BY_ZERO,
+ "divide by zero");
+ return FALSE;
+ }
+
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, g_value_get_double (left) / denom);
+
+ return TRUE;
+}
+
+static gboolean
+unary_minus_double (const GValue *left,
+ const GValue *right,
+ GValue *return_value,
+ GError **error)
+{
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, -g_value_get_double (left));
+ return TRUE;
+}
+
+static gboolean
+mul_double_string (const GValue *left,
+ const GValue *right,
+ GValue *return_value,
+ GError **error)
+{
+ GString *str;
+ gint v;
+ gint i;
+
+ str = g_string_new (NULL);
+ v = g_value_get_double (left);
+
+ for (i = 0; i < v; i++)
+ g_string_append (str, g_value_get_string (right));
+
+ g_value_init (return_value, G_TYPE_STRING);
+ g_value_take_string (return_value, g_string_free (str, FALSE));
+
+ return TRUE;
+}
+
+static gboolean
+mul_string_double (const GValue *left,
+ const GValue *right,
+ GValue *return_value,
+ GError **error)
+{
+ return mul_double_string (right, left, return_value, error);
+}
+
+static gboolean
+add_string_string (const GValue *left,
+ const GValue *right,
+ GValue *return_value,
+ GError **error)
+{
+ g_value_init (return_value, G_TYPE_STRING);
+ g_value_take_string (return_value,
+ g_strdup_printf ("%s%s",
+ g_value_get_string (left),
+ g_value_get_string (right)));
+ return TRUE;
+}
+
+#define SIMPLE_OP_FUNC(func_name, ret_type, set_func, get_left, op, get_right) \
+static gboolean \
+func_name (const GValue *left, \
+ const GValue *right, \
+ GValue *return_value, \
+ GError **error) \
+{ \
+ g_value_init (return_value, ret_type); \
+ g_value_##set_func (return_value, \
+ g_value_##get_left (left) \
+ op \
+ g_value_##get_right (right)); \
+ return TRUE; \
+}
+
+SIMPLE_OP_FUNC (add_double_double, G_TYPE_DOUBLE, set_double, get_double, +, get_double)
+SIMPLE_OP_FUNC (sub_double_double, G_TYPE_DOUBLE, set_double, get_double, -, get_double)
+SIMPLE_OP_FUNC (mul_double_double, G_TYPE_DOUBLE, set_double, get_double, *, get_double)
+SIMPLE_OP_FUNC (lt_double_double, G_TYPE_BOOLEAN, set_boolean, get_double, <, get_double)
+SIMPLE_OP_FUNC (lte_double_double, G_TYPE_BOOLEAN, set_boolean, get_double, <=, get_double)
+SIMPLE_OP_FUNC (gt_double_double, G_TYPE_BOOLEAN, set_boolean, get_double, >, get_double)
+SIMPLE_OP_FUNC (gte_double_double, G_TYPE_BOOLEAN, set_boolean, get_double, >=, get_double)
+
+#undef SIMPLE_OP_FUNC
+
+static GHashTable *
+build_dispatch_table (void)
+{
+ GHashTable *table;
+
+ table = g_hash_table_new (NULL, NULL);
+
+#define ADD_DISPATCH_FUNC(type, left, right, func) \
+ g_hash_table_insert(table, \
+ GINT_TO_POINTER(build_hash(type, left, right)),\
+ func)
+
+ ADD_DISPATCH_FUNC (TMPL_EXPR_ADD, G_TYPE_DOUBLE, G_TYPE_DOUBLE, add_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_ADD, G_TYPE_STRING, G_TYPE_STRING, add_string_string);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_SUB, G_TYPE_DOUBLE, G_TYPE_DOUBLE, sub_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_MUL, G_TYPE_DOUBLE, G_TYPE_DOUBLE, mul_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_DIV, G_TYPE_DOUBLE, G_TYPE_DOUBLE, div_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_UNARY_MINUS, G_TYPE_DOUBLE, 0, unary_minus_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_LT, G_TYPE_DOUBLE, G_TYPE_DOUBLE, lt_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_GT, G_TYPE_DOUBLE, G_TYPE_DOUBLE, gt_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_LTE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, lte_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_GTE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, gte_double_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_MUL, G_TYPE_STRING, G_TYPE_DOUBLE, mul_string_double);
+ ADD_DISPATCH_FUNC (TMPL_EXPR_MUL, G_TYPE_DOUBLE, G_TYPE_STRING, mul_double_string);
+
+#undef ADD_DISPATCH_FUNC
+
+ return table;
+}
+
+gboolean
+tmpl_expr_eval (TmplExpr *node,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error)
+{
+ g_return_val_if_fail (node != NULL, FALSE);
+ g_return_val_if_fail (scope != NULL, FALSE);
+ g_return_val_if_fail (return_value != NULL, FALSE);
+ g_return_val_if_fail (G_VALUE_TYPE (return_value) == G_TYPE_INVALID, FALSE);
+
+ if (g_once_init_enter (&fast_dispatch))
+ g_once_init_leave (&fast_dispatch, build_dispatch_table ());
+
+ return tmpl_expr_eval_internal (node, scope, return_value, error);
+}
+
+static gboolean
+builtin_abs (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ throw_type_mismatch (error, value, NULL, "not implemented");
+ return FALSE;
+}
+
+static gboolean
+builtin_ceil (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ if (G_VALUE_HOLDS_DOUBLE (value))
+ {
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, ceil (g_value_get_double (value)));
+ return TRUE;
+ }
+
+ throw_type_mismatch (error, value, NULL, "requires double parameter");
+
+ return FALSE;
+}
+
+static gboolean
+builtin_floor (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ if (G_VALUE_HOLDS_DOUBLE (value))
+ {
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, floor (g_value_get_double (value)));
+ return TRUE;
+ }
+
+ throw_type_mismatch (error, value, NULL, "requires double parameter");
+
+ return FALSE;
+}
+
+static gboolean
+builtin_log (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ if (G_VALUE_HOLDS_DOUBLE (value))
+ {
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, log (g_value_get_double (value)));
+ return TRUE;
+ }
+
+ throw_type_mismatch (error, value, NULL, "requires double parameter");
+
+ return FALSE;
+}
+
+static gboolean
+builtin_print (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ gchar *repr;
+
+ repr = tmpl_value_repr (value);
+ g_print ("%s\n", repr);
+ g_free (repr);
+
+ g_value_init (return_value, G_TYPE_BOOLEAN);
+ g_value_set_boolean (return_value, TRUE);
+
+ return TRUE;
+}
+
+static gboolean
+builtin_sqrt (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ if (G_VALUE_HOLDS_DOUBLE (value))
+ {
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, sqrt (g_value_get_double (value)));
+ return TRUE;
+ }
+
+ throw_type_mismatch (error, value, NULL, "requires double parameter");
+
+ return FALSE;
+}
+
+static gboolean
+builtin_hex (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ if (G_VALUE_HOLDS_DOUBLE (value))
+ {
+ gchar *str = g_strdup_printf ("0x%lx", (gint64)g_value_get_double (value));
+ g_value_init (return_value, G_TYPE_STRING);
+ g_value_take_string (return_value, str);
+ return TRUE;
+ }
+
+ throw_type_mismatch (error, value, NULL, "requires number parameter");
+
+ return FALSE;
+}
+
+static gboolean
+builtin_repr (const GValue *value,
+ GValue *return_value,
+ GError **error)
+{
+ g_value_init (return_value, G_TYPE_STRING);
+ g_value_take_string (return_value, tmpl_value_repr (value));
+ return TRUE;
+}
diff --git a/contrib/tmpl/tmpl-expr-node.c b/contrib/tmpl/tmpl-expr-node.c
new file mode 100644
index 0000000..e2cca85
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-node.c
@@ -0,0 +1,112 @@
+/* tmpl-expr-node.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-expr-node"
+
+#include "tmpl-debug.h"
+#include "tmpl-expr-node.h"
+
+struct _TmplExprNode
+{
+ TmplNode parent_instance;
+ TmplExpr *expr;
+};
+
+G_DEFINE_TYPE (TmplExprNode, tmpl_expr_node, TMPL_TYPE_NODE)
+
+static gboolean
+tmpl_expr_node_accept (TmplNode *node,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TMPL_ENTRY;
+ /* no children */
+ TMPL_RETURN (TRUE);
+}
+
+static void
+tmpl_expr_node_visit_children (TmplNode *node,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ TMPL_ENTRY;
+ /* no children */
+ TMPL_EXIT;
+}
+
+static void
+tmpl_expr_node_finalize (GObject *object)
+{
+ TmplExprNode *self = (TmplExprNode *)object;
+
+ g_clear_pointer (&self->expr, tmpl_expr_unref);
+
+ G_OBJECT_CLASS (tmpl_expr_node_parent_class)->finalize (object);
+}
+
+static void
+tmpl_expr_node_class_init (TmplExprNodeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ TmplNodeClass *node_class = TMPL_NODE_CLASS (klass);
+
+ object_class->finalize = tmpl_expr_node_finalize;
+
+ node_class->accept = tmpl_expr_node_accept;
+ node_class->visit_children = tmpl_expr_node_visit_children;
+}
+
+static void
+tmpl_expr_node_init (TmplExprNode *self)
+{
+}
+
+/**
+ * tmpl_expr_node_new:
+ * @expr: (transfer full): The expression
+ *
+ * Creates a new node, stealing the reference to @expr.
+ *
+ * Returns: (transfer full): A #TmplExprNode
+ */
+TmplNode *
+tmpl_expr_node_new (TmplExpr *expr)
+{
+ TmplExprNode *self;
+
+ self = g_object_new (TMPL_TYPE_EXPR_NODE, NULL);
+ self->expr = expr;
+
+ return TMPL_NODE (self);
+}
+
+/**
+ * tmpl_expr_node_get_expr:
+ *
+ * Gets the expression.
+ *
+ * Returns: (transfer none): A #TmplExpr.
+ */
+TmplExpr *
+tmpl_expr_node_get_expr (TmplExprNode *self)
+{
+ g_return_val_if_fail (TMPL_IS_EXPR_NODE (self), NULL);
+
+ return self->expr;
+}
diff --git a/contrib/tmpl/tmpl-expr-node.h b/contrib/tmpl/tmpl-expr-node.h
new file mode 100644
index 0000000..4ba2910
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-node.h
@@ -0,0 +1,36 @@
+/* tmpl-expr-node.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_EXPR_NODE_H
+#define TMPL_EXPR_NODE_H
+
+#include "tmpl-expr.h"
+#include "tmpl-node.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_EXPR_NODE (tmpl_expr_node_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplExprNode, tmpl_expr_node, TMPL, EXPR_NODE, TmplNode)
+
+TmplNode *tmpl_expr_node_new (TmplExpr *expr);
+TmplExpr *tmpl_expr_node_get_expr (TmplExprNode *self);
+
+G_END_DECLS
+
+#endif /* TMPL_EXPR_NODE_H */
diff --git a/contrib/tmpl/tmpl-expr-parser-private.h b/contrib/tmpl/tmpl-expr-parser-private.h
new file mode 100644
index 0000000..a642657
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-parser-private.h
@@ -0,0 +1,48 @@
+/* tmpl-expr-parser-private.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_EXPR_PARSER_PRIVATE_H
+#define TMPL_EXPR_PARSER_PRIVATE_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+ TmplExpr *ast;
+ TmplScope *scope;
+ gpointer scanner;
+ gchar *error_str;
+ gint error_line;
+ guint reached_eof : 1;
+} TmplExprParser;
+
+void tmpl_expr_parser_destroy (TmplExprParser *parser);
+void tmpl_expr_parser_flush (TmplExprParser *parser);
+void tmpl_expr_parser_error (TmplExprParser *parser,
+ const char *message);
+gboolean tmpl_expr_parser_parse_string (TmplExprParser *parser,
+ const gchar *input,
+ GError **error);
+gboolean tmpl_expr_parser_init (TmplExprParser *parser,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* TMPL_EXPR_PARSER_PRIVATE_H */
diff --git a/contrib/tmpl/tmpl-expr-parser.y b/contrib/tmpl/tmpl-expr-parser.y
new file mode 100644
index 0000000..721e812
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-parser.y
@@ -0,0 +1,200 @@
+%pure-parser
+%name-prefix "tmpl_expr_parser_"
+%defines
+%error-verbose
+%parse-param { TmplExprParser *parser }
+%lex-param { void *scanner }
+
+%{
+# include <glib/gprintf.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+
+# include "tmpl-expr.h"
+# include "tmpl-expr-private.h"
+# include "tmpl-expr-parser-private.h"
+%}
+
+%union {
+ TmplExpr *a; /* ast node */
+ double d; /* number */
+ char *s; /* symbol/string */
+ GPtrArray *sl; /* symlist */
+ TmplExprBuiltin fn; /* builtin call */
+ int b; /* boolean */
+ int cmp; /* comparison */
+}
+
+%{
+int tmpl_expr_parser_lex (YYSTYPE *, void *scanner);
+
+void
+tmpl_expr_parser_error (TmplExprParser *parser,
+ const gchar *message)
+{
+ g_assert (parser != NULL);
+ g_assert (message != NULL);
+
+ g_clear_pointer (&parser->ast, tmpl_expr_unref);
+
+ g_free (parser->error_str);
+ parser->error_str = g_strdup (message);
+}
+
+# define scanner parser->scanner
+%}
+
+%token <s> REQUIRE VERSION
+
+%token <b> BOOL
+%token <d> NUMBER
+%token <s> NAME STRING_LITERAL
+%token <fn> BUILTIN
+%token EOL
+
+%token IF THEN ELSE WHILE DO FUNC
+
+%nonassoc <cmp> CMP
+%right '='
+%left '+' '-'
+%left '*' '/'
+
+%nonassoc '|' UMINUS
+
+%type <a> exp stmt list explist
+%type <sl> symlist
+
+%start expr
+
+%%
+
+expr: /* nothing */ EOL {
+ parser->ast = NULL;
+ YYACCEPT;
+ }
+ | stmt EOL {
+ parser->ast = $1;
+ YYACCEPT;
+ }
+ | FUNC NAME '(' symlist ')' '=' list EOL {
+ /* todo: add ast node to define the expr on the scope
+ * when evaluated.
+ */
+ //tmpl_scope_add_user_func (parser->scope, $2, $4, $7);
+ parser->ast = NULL;
+ YYACCEPT;
+ }
+ | FUNC NAME '(' ')' '=' list EOL {
+ /* todo: add ast node to define the expr on the scope
+ * when evaluated.
+ */
+ //tmpl_scope_add_user_func (parser->scope, $2, NULL, $6);
+ parser->ast = NULL;
+ YYACCEPT;
+ }
+;
+
+stmt: IF exp THEN list {
+ $$ = tmpl_expr_new_flow (TMPL_EXPR_IF, $2, $4, NULL);
+ }
+ | IF exp THEN list ELSE list {
+ $$ = tmpl_expr_new_flow (TMPL_EXPR_IF, $2, $4, $6);
+ }
+ | WHILE exp DO list {
+ $$ = tmpl_expr_new_flow (TMPL_EXPR_WHILE, $2, $4, NULL);
+ }
+ | exp
+;
+
+list: /* nothing */ { $$ = NULL; }
+ | stmt ';' list {
+ if ($3 == NULL)
+ $$ = $1;
+ else
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_STMT_LIST, $1, $3);
+ }
+;
+
+exp: exp CMP exp {
+ $$ = tmpl_expr_new_simple ($2, $1, $3);
+ }
+ | exp '+' exp {
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_ADD, $1, $3);
+ }
+ | exp '-' exp {
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_SUB, $1, $3);
+ }
+ | exp '*' exp {
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_MUL, $1, $3);
+ }
+ | exp '/' exp {
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_DIV, $1, $3);
+ }
+ | '(' exp ')' {
+ $$ = $2;
+ }
+ | '-' exp %prec UMINUS {
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_UNARY_MINUS, $2, NULL);
+ }
+ | NUMBER {
+ $$ = tmpl_expr_new_number ($1);
+ }
+ | BOOL {
+ $$ = tmpl_expr_new_boolean ($1);
+ }
+ | STRING_LITERAL {
+ $$ = tmpl_expr_new_string ($1+1, strlen($1) - 2);
+ }
+ | NAME {
+ $$ = tmpl_expr_new_symbol_ref ($1);
+ }
+ | NAME '=' exp {
+ $$ = tmpl_expr_new_symbol_assign ($1, $3);
+ }
+ | exp '.' NAME '(' ')' {
+ $$ = tmpl_expr_new_gi_call ($1, $3, NULL);
+ }
+ | exp '.' NAME '(' explist ')' {
+ $$ = tmpl_expr_new_gi_call ($1, $3, $5);
+ }
+ | exp '.' NAME {
+ $$ = tmpl_expr_new_getattr ($1, $3);
+ }
+ | exp '.' NAME '=' exp {
+ $$ = tmpl_expr_new_setattr ($1, $3, $5);
+ }
+ | BUILTIN '(' explist ')' {
+ $$ = tmpl_expr_new_fn_call ($1, $3);
+ }
+ | NAME '(' explist ')' {
+ $$ = tmpl_expr_new_user_fn_call ($1, $3);
+ }
+ | NAME '(' ')' {
+ $$ = tmpl_expr_new_user_fn_call ($1, NULL);
+ }
+ | REQUIRE NAME {
+ $$ = tmpl_expr_new_require ($2, NULL);
+ }
+ | REQUIRE NAME VERSION STRING_LITERAL {
+ char *vstr = g_strndup ($4+1, strlen($4)-2);
+ $$ = tmpl_expr_new_require ($2, vstr);
+ g_free (vstr);
+ }
+;
+
+explist: exp
+ | exp ',' explist {
+ $$ = tmpl_expr_new_simple (TMPL_EXPR_STMT_LIST, $1, $3);
+ }
+;
+
+symlist: NAME {
+ $$ = g_ptr_array_new_with_free_func (g_free);
+ g_ptr_array_add ($$, $1);
+ }
+ | NAME ',' symlist {
+ g_ptr_array_insert ($3, 0, $1);
+ }
+;
+
diff --git a/contrib/tmpl/tmpl-expr-private.h b/contrib/tmpl/tmpl-expr-private.h
new file mode 100644
index 0000000..57a33d6
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-private.h
@@ -0,0 +1,158 @@
+/* tmpl-expr-private.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_EXPR_PRIVATE_H
+#define TMPL_EXPR_PRIVATE_H
+
+#include "tmpl-expr.h"
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ TmplExpr *left;
+ TmplExpr *right;
+} TmplExprSimple;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ TmplExpr *object;
+ gchar *name;
+ TmplExpr *params;
+} TmplExprGiCall;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ TmplExprBuiltin builtin;
+ TmplExpr *param;
+} TmplExprFnCall;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *symbol;
+ TmplExpr *params;
+} TmplExprUserFnCall;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ TmplExpr *condition;
+ TmplExpr *primary;
+ TmplExpr *secondary;
+} TmplExprFlow;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gdouble number;
+} TmplExprNumber;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ guint value: 1;
+} TmplExprBoolean;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *value;
+} TmplExprString;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *symbol;
+} TmplExprSymbolRef;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *symbol;
+ TmplExpr *right;
+} TmplExprSymbolAssign;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *attr;
+ TmplExpr *left;
+} TmplExprGetattr;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *attr;
+ TmplExpr *left;
+ TmplExpr *right;
+} TmplExprSetattr;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+ gchar *name;
+ gchar *version;
+} TmplExprRequire;
+
+typedef struct
+{
+ TmplExprType type;
+ volatile gint ref_count;
+} TmplExprAny;
+
+union _TmplExpr
+{
+ TmplExprAny any;
+ TmplExprSimple simple;
+ TmplExprGiCall gi_call;
+ TmplExprFnCall fn_call;
+ TmplExprUserFnCall user_fn_call;
+ TmplExprFlow flow;
+ TmplExprNumber number;
+ TmplExprString string;
+ TmplExprSymbolRef sym_ref;
+ TmplExprSymbolAssign sym_assign;
+ TmplExprGetattr getattr;
+ TmplExprSetattr setattr;
+ TmplExprRequire require;
+};
+
+G_END_DECLS
+
+#endif /* TMPL_EXPR_PRIVATE_H */
diff --git a/contrib/tmpl/tmpl-expr-scanner.l b/contrib/tmpl/tmpl-expr-scanner.l
new file mode 100644
index 0000000..d10cc77
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-scanner.l
@@ -0,0 +1,179 @@
+%option reentrant
+%option prefix="tmpl_expr_parser_"
+%option bison-bridge
+%option noyywrap
+%option yylineno
+%option nodefault
+
+%option header-file="tmpl-expr-scanner.h"
+%option outfile="tmpl-expr-scanner.c"
+
+%{
+# include "tmpl-error.h"
+# include "tmpl-expr-private.h"
+# include "tmpl-expr-parser-private.h"
+# include "tmpl-expr-parser.h"
+%}
+
+%option extra-type="TmplExprParser *"
+
+ /* float exponent */
+EXP ([Ee][-+]?[0-9]+)
+
+%%
+
+%{
+TmplExprParser *parser = yyextra;
+%}
+
+ /* single character ops */
+"+" |
+"-" |
+"*" |
+"/" |
+"=" |
+"," |
+"." |
+";" |
+"(" |
+")" { return yytext [0]; }
+
+ /* comparison ops */
+">" { yylval->cmp = TMPL_EXPR_GT; return CMP; }
+"<" { yylval->cmp = TMPL_EXPR_LT; return CMP; }
+"!=" { yylval->cmp = TMPL_EXPR_NE; return CMP; }
+"==" { yylval->cmp = TMPL_EXPR_EQ; return CMP; }
+">=" { yylval->cmp = TMPL_EXPR_GTE; return CMP; }
+"<=" { yylval->cmp = TMPL_EXPR_LTE; return CMP; }
+
+ /* keywords */
+"if" { return IF; }
+"then" { return THEN; }
+"else" { return ELSE; }
+"while" { return WHILE; }
+"do" { return DO; }
+"func" { return FUNC; }
+"require" { return REQUIRE; }
+"version" { return VERSION; }
+
+"true" { yylval->b = 1; return BOOL; }
+"false" { yylval->b = 0; return BOOL; }
+
+ /* builtin functions */
+"ceil" { yylval->fn = TMPL_EXPR_BUILTIN_CEIL; return BUILTIN; }
+"floor" { yylval->fn = TMPL_EXPR_BUILTIN_FLOOR; return BUILTIN; }
+"hex" { yylval->fn = TMPL_EXPR_BUILTIN_HEX; return BUILTIN; }
+"log" { yylval->fn = TMPL_EXPR_BUILTIN_LOG; return BUILTIN; }
+"print" { yylval->fn = TMPL_EXPR_BUILTIN_PRINT; return BUILTIN; }
+"repr" { yylval->fn = TMPL_EXPR_BUILTIN_REPR; return BUILTIN; }
+"sqrt" { yylval->fn = TMPL_EXPR_BUILTIN_SQRT; return BUILTIN; }
+
+ /* string literals */
+L?\"(\\.|[^\\"])*\" { yylval->s = yytext; return STRING_LITERAL; }
+
+ /* names */
+[a-zA-Z_][a-zA-Z0-9_]* {
+ yylval->s = g_strdup (yytext);
+ return NAME;
+}
+
+[0-9]+"."[0-9]*{EXP}? |
+"."?[0-9]+{EXP}? { yylval->d = atof(yytext); return NUMBER; }
+
+"//".*
+[ \t] /* ignore whitespace */
+\\n { printf ("c> "); }
+"\n" { return EOL; }
+<<EOF>> { parser->reached_eof = TRUE; return EOL; }
+. {
+ g_free (parser->error_str);
+ parser->error_str = g_strdup (yytext);
+ parser->error_line = yylineno;
+ }
+
+%%
+
+void
+tmpl_expr_parser_init_scanner (TmplExprParser *parser)
+{
+ g_assert (parser != NULL);
+
+ yylex_init (&parser->scanner);
+ yyset_extra (parser, parser->scanner);
+}
+
+void
+tmpl_expr_parser_destroy_scanner (TmplExprParser *parser)
+{
+ g_assert (parser != NULL);
+
+ yylex_destroy (parser->scanner);
+}
+
+void
+tmpl_expr_parser_flush (TmplExprParser *parser)
+{
+ struct yyguts_t *yyg;
+
+ g_assert (parser != NULL);
+
+ yyg = (struct yyguts_t *)parser->scanner;
+ tmpl_expr_parser__flush_buffer (YY_CURRENT_BUFFER, parser->scanner);
+}
+
+gboolean
+tmpl_expr_parser_parse_string (TmplExprParser *self,
+ const gchar *input,
+ GError **error)
+{
+ YY_BUFFER_STATE buf;
+ gint ret;
+
+ g_return_val_if_fail (self != NULL, FALSE);
+
+ buf = tmpl_expr_parser__scan_string (input, self->scanner);
+ ret = tmpl_expr_parser_parse (self);
+ tmpl_expr_parser__delete_buffer (buf, self->scanner);
+
+ if (self->error_str)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "%s", self->error_str);
+ return FALSE;
+ }
+
+ if (ret == 0)
+ return TRUE;
+
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Failed to parse expression");
+
+ return FALSE;
+}
+
+gboolean
+tmpl_expr_parser_init (TmplExprParser *self,
+ GError **error)
+{
+ g_return_val_if_fail (self != NULL, FALSE);
+
+ memset (self, 0, sizeof *self);
+ tmpl_expr_parser_init_scanner (self);
+
+ return TRUE;
+}
+
+void
+tmpl_expr_parser_destroy (TmplExprParser *self)
+{
+ if (self != NULL)
+ {
+ tmpl_expr_parser_destroy_scanner (self);
+ g_clear_pointer (&self->ast, tmpl_expr_unref);
+ g_clear_pointer (&self->error_str, g_free);
+ }
+}
diff --git a/contrib/tmpl/tmpl-expr-types.h b/contrib/tmpl/tmpl-expr-types.h
new file mode 100644
index 0000000..1dab4cd
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr-types.h
@@ -0,0 +1,93 @@
+/* tmpl-expr-types.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_EXPR_TYPES_H
+#define TMPL_EXPR_TYPES_H
+
+#include <glib-object.h>
+
+#include "tmpl-enums.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_EXPR (tmpl_expr_get_type())
+#define TMPL_TYPE_SCOPE (tmpl_scope_get_type())
+#define TMPL_TYPE_SYMBOL (tmpl_symbol_get_type())
+
+typedef union _TmplExpr TmplExpr;
+typedef struct _TmplScope TmplScope;
+typedef struct _TmplSymbol TmplSymbol;
+
+typedef enum
+{
+ TMPL_SYMBOL_EXPR,
+ TMPL_SYMBOL_VALUE,
+} TmplSymbolType;
+
+typedef enum
+{
+ TMPL_EXPR_ADD = 1,
+ TMPL_EXPR_SUB,
+ TMPL_EXPR_MUL,
+ TMPL_EXPR_DIV,
+ TMPL_EXPR_BOOLEAN,
+ TMPL_EXPR_NUMBER,
+ TMPL_EXPR_STRING,
+ TMPL_EXPR_GT,
+ TMPL_EXPR_LT,
+ TMPL_EXPR_NE,
+ TMPL_EXPR_EQ,
+ TMPL_EXPR_GTE,
+ TMPL_EXPR_LTE,
+ TMPL_EXPR_UNARY_MINUS,
+ TMPL_EXPR_STMT_LIST,
+ TMPL_EXPR_IF,
+ TMPL_EXPR_WHILE,
+ TMPL_EXPR_SYMBOL_REF,
+ TMPL_EXPR_SYMBOL_ASSIGN,
+ TMPL_EXPR_FN_CALL,
+ TMPL_EXPR_USER_FN_CALL,
+ TMPL_EXPR_GETATTR,
+ TMPL_EXPR_SETATTR,
+ TMPL_EXPR_GI_CALL,
+ TMPL_EXPR_REQUIRE,
+} TmplExprType;
+
+typedef enum
+{
+ TMPL_EXPR_BUILTIN_ABS,
+ TMPL_EXPR_BUILTIN_CEIL,
+ TMPL_EXPR_BUILTIN_FLOOR,
+ TMPL_EXPR_BUILTIN_HEX,
+ TMPL_EXPR_BUILTIN_LOG,
+ TMPL_EXPR_BUILTIN_PRINT,
+ TMPL_EXPR_BUILTIN_REPR,
+ TMPL_EXPR_BUILTIN_SQRT,
+} TmplExprBuiltin;
+
+GType tmpl_expr_get_type (void);
+GType tmpl_scope_get_type (void);
+GType tmpl_symbol_get_type (void);
+
+G_END_DECLS
+
+#endif /* TMPL_EXPR_TYPES_H */
diff --git a/contrib/tmpl/tmpl-expr.c b/contrib/tmpl/tmpl-expr.c
new file mode 100644
index 0000000..4605ba4
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr.c
@@ -0,0 +1,337 @@
+/* tmpl-expr.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tmpl-expr.h"
+#include "tmpl-expr-private.h"
+#include "tmpl-expr-parser-private.h"
+
+static gpointer tmpl_expr_new (TmplExprType type);
+static void tmpl_expr_destroy (TmplExpr *expr);
+
+G_DEFINE_BOXED_TYPE (TmplExpr, tmpl_expr, tmpl_expr_ref, tmpl_expr_unref)
+
+TmplExpr *
+tmpl_expr_ref (TmplExpr *self)
+{
+ g_return_val_if_fail (self != NULL, NULL);
+ g_return_val_if_fail (self->any.ref_count > 0, NULL);
+
+ g_atomic_int_inc (&self->any.ref_count);
+
+ return self;
+}
+
+void
+tmpl_expr_unref (TmplExpr *self)
+{
+ g_return_if_fail (self != NULL);
+ g_return_if_fail (self->any.ref_count > 0);
+
+ if (g_atomic_int_dec_and_test (&self->any.ref_count))
+ tmpl_expr_destroy (self);
+}
+
+static gpointer
+tmpl_expr_new (TmplExprType type)
+{
+ TmplExpr *ret;
+
+ ret = g_slice_new0 (TmplExpr);
+ ret->any.type = type;
+ ret->any.ref_count = 1;
+
+ return ret;
+}
+
+static void
+tmpl_expr_destroy (TmplExpr *self)
+{
+ g_assert (self != NULL);
+ g_assert (self->any.ref_count == 0);
+
+ /* Set fields to NULL to aid in debugging. */
+
+ switch (self->any.type)
+ {
+ case TMPL_EXPR_ADD:
+ case TMPL_EXPR_DIV:
+ case TMPL_EXPR_EQ:
+ case TMPL_EXPR_GT:
+ case TMPL_EXPR_GTE:
+ case TMPL_EXPR_LT:
+ case TMPL_EXPR_LTE:
+ case TMPL_EXPR_MUL:
+ case TMPL_EXPR_NE:
+ case TMPL_EXPR_STMT_LIST:
+ case TMPL_EXPR_SUB:
+ case TMPL_EXPR_UNARY_MINUS:
+ case TMPL_EXPR_USER_FN_CALL:
+ g_clear_pointer (&self->simple.left, tmpl_expr_unref);
+ g_clear_pointer (&self->simple.right, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_GETATTR:
+ g_clear_pointer (&self->getattr.attr, g_free);
+ g_clear_pointer (&self->getattr.left, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_SETATTR:
+ g_clear_pointer (&self->setattr.attr, g_free);
+ g_clear_pointer (&self->setattr.left, tmpl_expr_unref);
+ g_clear_pointer (&self->setattr.right, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_BOOLEAN:
+ case TMPL_EXPR_NUMBER:
+ break;
+
+ case TMPL_EXPR_STRING:
+ g_clear_pointer (&self->string.value, g_free);
+ break;
+
+ case TMPL_EXPR_IF:
+ case TMPL_EXPR_WHILE:
+ g_clear_pointer (&self->flow.condition, tmpl_expr_unref);
+ g_clear_pointer (&self->flow.primary, tmpl_expr_unref);
+ g_clear_pointer (&self->flow.secondary, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_SYMBOL_REF:
+ g_clear_pointer (&self->sym_ref.symbol, g_free);
+ break;
+
+ case TMPL_EXPR_SYMBOL_ASSIGN:
+ g_clear_pointer (&self->sym_assign.symbol, g_free);
+ g_clear_pointer (&self->sym_assign.right, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_FN_CALL:
+ g_clear_pointer (&self->fn_call.param, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_GI_CALL:
+ g_clear_pointer (&self->gi_call.name, g_free);
+ g_clear_pointer (&self->gi_call.object, tmpl_expr_unref);
+ g_clear_pointer (&self->gi_call.params, tmpl_expr_unref);
+ break;
+
+ case TMPL_EXPR_REQUIRE:
+ g_clear_pointer (&self->require.name, g_free);
+ g_clear_pointer (&self->require.version, g_free);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ g_slice_free (TmplExpr, self);
+}
+
+TmplExpr *
+tmpl_expr_new_boolean (gboolean value)
+{
+ TmplExpr *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_BOOLEAN);
+ ((TmplExprBoolean *)ret)->value = !!value;
+
+ return ret;
+}
+
+TmplExpr *
+tmpl_expr_new_number (gdouble value)
+{
+ TmplExprNumber *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_NUMBER);
+ ret->number = value;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_string (const gchar *str,
+ gssize length)
+{
+ TmplExprString *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_STRING);
+
+ if (length < 0)
+ ret->value = g_strdup (str);
+ else
+ ret->value = g_strndup (str, length);
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_require (const gchar *typelib,
+ const gchar *version)
+{
+ TmplExprRequire *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_REQUIRE);
+ ret->name = g_strdup (typelib);
+ ret->version = g_strdup (version);
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_simple (TmplExprType type,
+ TmplExpr *left,
+ TmplExpr *right)
+{
+ TmplExprSimple *ret;
+
+ ret = tmpl_expr_new (type);
+ ret->left = left;
+ ret->right = right;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_flow (TmplExprType type,
+ TmplExpr *condition,
+ TmplExpr *primary,
+ TmplExpr *secondary)
+{
+ TmplExprFlow *ret;
+
+ ret = tmpl_expr_new (type);
+ ret->condition = condition;
+ ret->primary = primary;
+ ret->secondary = secondary;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_getattr (TmplExpr *left,
+ const gchar *attr)
+{
+ TmplExprGetattr *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_GETATTR);
+ ret->left = left;
+ ret->attr = g_strdup (attr);
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_setattr (TmplExpr *left,
+ const gchar *attr,
+ TmplExpr *right)
+{
+ TmplExprSetattr *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_SETATTR);
+ ret->left = left;
+ ret->attr = g_strdup (attr);
+ ret->right = right;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_symbol_ref (const gchar *symbol)
+{
+ TmplExprSymbolRef *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_SYMBOL_REF);
+ ret->symbol = g_strdup (symbol);
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_symbol_assign (const gchar *symbol,
+ TmplExpr *right)
+{
+ TmplExprSymbolAssign *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_SYMBOL_ASSIGN);
+ ret->symbol = g_strdup (symbol);
+ ret->right = right;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_fn_call (TmplExprBuiltin builtin,
+ TmplExpr *param)
+{
+ TmplExprFnCall *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_FN_CALL);
+ ret->builtin = builtin;
+ ret->param = param;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_user_fn_call (const gchar *symbol,
+ TmplExpr *params)
+{
+ TmplExprUserFnCall *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_USER_FN_CALL);
+ ret->symbol = g_strdup (symbol);
+ ret->params = params;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_new_gi_call (TmplExpr *object,
+ const gchar *name,
+ TmplExpr *params)
+{
+ TmplExprGiCall *ret;
+
+ ret = tmpl_expr_new (TMPL_EXPR_GI_CALL);
+ ret->object = object;
+ ret->name = g_strdup (name);
+ ret->params = params;
+
+ return (TmplExpr *)ret;
+}
+
+TmplExpr *
+tmpl_expr_from_string (const gchar *str,
+ GError **error)
+{
+ TmplExprParser parser = { 0 };
+ TmplExpr *ret = NULL;
+
+ g_return_val_if_fail (str != NULL, NULL);
+
+ if (!tmpl_expr_parser_init (&parser, error))
+ return NULL;
+
+ if (tmpl_expr_parser_parse_string (&parser, str, error))
+ ret = parser.ast, parser.ast = NULL;
+
+ tmpl_expr_parser_destroy (&parser);
+
+ return ret;
+}
diff --git a/contrib/tmpl/tmpl-expr.h b/contrib/tmpl/tmpl-expr.h
new file mode 100644
index 0000000..d6e2826
--- /dev/null
+++ b/contrib/tmpl/tmpl-expr.h
@@ -0,0 +1,69 @@
+/* tmpl-expr.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_EXPR_H
+#define TMPL_EXPR_H
+
+#include "tmpl-expr-types.h"
+
+G_BEGIN_DECLS
+
+TmplExpr *tmpl_expr_from_string (const gchar *str,
+ GError **error);
+TmplExpr *tmpl_expr_ref (TmplExpr *expr);
+void tmpl_expr_unref (TmplExpr *expr);
+gboolean tmpl_expr_eval (TmplExpr *expr,
+ TmplScope *scope,
+ GValue *return_value,
+ GError **error);
+TmplExpr *tmpl_expr_new_boolean (gboolean value);
+TmplExpr *tmpl_expr_new_getattr (TmplExpr *left,
+ const gchar *attr);
+TmplExpr *tmpl_expr_new_setattr (TmplExpr *left,
+ const gchar *attr,
+ TmplExpr *right);
+TmplExpr *tmpl_expr_new_require (const gchar *typelib,
+ const gchar *version);
+TmplExpr *tmpl_expr_new_simple (TmplExprType type,
+ TmplExpr *left,
+ TmplExpr *right);
+TmplExpr *tmpl_expr_new_string (const gchar *value,
+ gssize length);
+TmplExpr *tmpl_expr_new_number (gdouble value);
+TmplExpr *tmpl_expr_new_gi_call (TmplExpr *left,
+ const gchar *name,
+ TmplExpr *params);
+TmplExpr *tmpl_expr_new_fn_call (TmplExprBuiltin builtin,
+ TmplExpr *param);
+TmplExpr *tmpl_expr_new_user_fn_call (const gchar *name,
+ TmplExpr *param);
+TmplExpr *tmpl_expr_new_flow (TmplExprType type,
+ TmplExpr *condition,
+ TmplExpr *primary,
+ TmplExpr *secondary);
+TmplExpr *tmpl_expr_new_symbol_ref (const gchar *symbol);
+TmplExpr *tmpl_expr_new_symbol_assign (const gchar *symbol,
+ TmplExpr *right);
+
+G_END_DECLS
+
+#endif /* TMPL_EXPR_H */
diff --git a/contrib/tmpl/tmpl-gi-private.h b/contrib/tmpl/tmpl-gi-private.h
new file mode 100644
index 0000000..d7cd8b1
--- /dev/null
+++ b/contrib/tmpl/tmpl-gi-private.h
@@ -0,0 +1,38 @@
+/* tmpl-gi-private.h
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_GI_PRIVATE_H
+#define TMPL_GI_PRIVATE_H
+
+#include <girepository.h>
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_TYPELIB (tmpl_typelib_get_type())
+
+GType tmpl_typelib_get_type (void);
+gboolean tmpl_gi_argument_from_g_value (const GValue *value,
+ GITypeInfo *type_info,
+ GIArgument *arg,
+ GError **error);
+gboolean tmpl_gi_argument_to_g_value (GValue *value,
+ GITypeInfo *type_info,
+ GIArgument *arg,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* TMPL_GI_PRIVATE_H */
diff --git a/contrib/tmpl/tmpl-gi.c b/contrib/tmpl/tmpl-gi.c
new file mode 100644
index 0000000..35d8b5c
--- /dev/null
+++ b/contrib/tmpl/tmpl-gi.c
@@ -0,0 +1,393 @@
+/* tmpl-gi.c
+ *
+ * Copyright PyGObject authors
+ * Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tmpl-error.h"
+#include "tmpl-gi-private.h"
+
+G_DEFINE_POINTER_TYPE (GisTypelib, tmpl_typelib)
+
+#define return_type_mismatch(value, type) \
+ G_STMT_START { \
+ g_set_error (error, \
+ TMPL_ERROR, \
+ TMPL_ERROR_TYPE_MISMATCH, \
+ "Expected %s, got %s", \
+ g_type_name (type), G_VALUE_TYPE_NAME (value)); \
+ return FALSE; \
+ } G_STMT_END
+
+#define return_if_not_type(value, type) \
+ G_STMT_START { \
+ if (!G_VALUE_HOLDS (value, type)) \
+ return_type_mismatch(value, type); \
+ } G_STMT_END
+
+gboolean
+tmpl_gi_argument_from_g_value (const GValue *value,
+ GITypeInfo *type_info,
+ GIArgument *arg,
+ GError **error)
+{
+ GITypeTag type_tag = g_type_info_get_tag (type_info);
+
+ /* For the long handling: long can be equivalent to
+ * int32 or int64, depending on the architecture, but
+ * gi doesn't tell us (and same for ulong)
+ */
+
+ if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_TYPE_MISMATCH,
+ "uninitialized value");
+ return FALSE;
+ }
+
+ switch (type_tag)
+ {
+ case GI_TYPE_TAG_BOOLEAN:
+ return_if_not_type (value, G_TYPE_BOOLEAN);
+ arg->v_boolean = g_value_get_boolean (value);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT8:
+ return_if_not_type (value, G_TYPE_CHAR);
+ arg->v_int8 = g_value_get_schar (value);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT16:
+ case GI_TYPE_TAG_INT32:
+ if (G_VALUE_HOLDS (value, G_TYPE_LONG))
+ arg->v_int = g_value_get_long (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_INT))
+ arg->v_int = g_value_get_int (value);
+ else
+ return_type_mismatch (value, G_TYPE_INT);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT64:
+ if (G_VALUE_HOLDS (value, G_TYPE_LONG))
+ arg->v_int64 = g_value_get_long (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_INT64))
+ arg->v_int64 = g_value_get_int64 (value);
+ else
+ return_type_mismatch (value, G_TYPE_INT64);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT8:
+ if (G_VALUE_HOLDS (value, G_TYPE_UCHAR))
+ arg->v_uint8 = g_value_get_uchar (value);
+ else
+ return_type_mismatch (value, G_TYPE_UCHAR);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT16:
+ case GI_TYPE_TAG_UINT32:
+ if (G_VALUE_HOLDS (value, G_TYPE_ULONG))
+ arg->v_uint = g_value_get_ulong (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_UINT))
+ arg->v_uint = g_value_get_uint (value);
+ else
+ return_type_mismatch (value, G_TYPE_UINT);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT64:
+ if (G_VALUE_HOLDS (value, G_TYPE_ULONG))
+ arg->v_uint64 = g_value_get_ulong (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_UINT64))
+ arg->v_uint64 = g_value_get_uint64 (value);
+ else
+ return_type_mismatch (value, G_TYPE_UINT64);
+ return TRUE;
+
+ case GI_TYPE_TAG_UNICHAR:
+ if (G_VALUE_HOLDS (value, G_TYPE_CHAR))
+ arg->v_uint32 = g_value_get_schar (value);
+ else
+ return_type_mismatch (value, G_TYPE_CHAR);
+ return TRUE;
+
+ case GI_TYPE_TAG_FLOAT:
+ if (G_VALUE_HOLDS (value, G_TYPE_FLOAT))
+ arg->v_float = g_value_get_float (value);
+ else
+ return_type_mismatch (value, G_TYPE_FLOAT);
+ return TRUE;
+
+ case GI_TYPE_TAG_DOUBLE:
+ if (G_VALUE_HOLDS (value, G_TYPE_DOUBLE))
+ arg->v_double = g_value_get_double (value);
+ else
+ return_type_mismatch (value, G_TYPE_DOUBLE);
+ return TRUE;
+
+ case GI_TYPE_TAG_GTYPE:
+ if (G_VALUE_HOLDS (value, G_TYPE_GTYPE))
+ arg->v_long = g_value_get_gtype (value);
+ else
+ return_type_mismatch (value, G_TYPE_GTYPE);
+ return TRUE;
+
+ case GI_TYPE_TAG_UTF8:
+ case GI_TYPE_TAG_FILENAME:
+ /* Callers are responsible for ensuring the GValue stays alive
+ * long enough for the string to be copied. */
+ if (G_VALUE_HOLDS (value, G_TYPE_STRING))
+ arg->v_string = (char *)g_value_get_string (value);
+ else
+ return_type_mismatch (value, G_TYPE_STRING);
+ return TRUE;
+
+ case GI_TYPE_TAG_GLIST:
+ case GI_TYPE_TAG_GSLIST:
+ case GI_TYPE_TAG_ARRAY:
+ case GI_TYPE_TAG_GHASH:
+ if (G_VALUE_HOLDS_BOXED (value))
+ arg->v_pointer = g_value_get_boxed (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_POINTER))
+ /* e. g. GSettings::change-event */
+ arg->v_pointer = g_value_get_pointer (value);
+ else
+ return_type_mismatch (value, G_TYPE_POINTER);
+ return TRUE;
+
+ case GI_TYPE_TAG_INTERFACE:
+ {
+ GIBaseInfo *info;
+ GIInfoType info_type;
+
+ info = g_type_info_get_interface (type_info);
+ info_type = g_base_info_get_type (info);
+
+ g_base_info_unref (info);
+
+ switch (info_type)
+ {
+ case GI_INFO_TYPE_FLAGS:
+ if (G_VALUE_HOLDS (value, G_TYPE_FLAGS))
+ arg->v_uint = g_value_get_flags (value);
+ else
+ return_type_mismatch (value, G_TYPE_FLAGS);
+ return TRUE;
+
+ case GI_INFO_TYPE_ENUM:
+ arg->v_int = g_value_get_enum (value);
+ return TRUE;
+
+ case GI_INFO_TYPE_INTERFACE:
+ case GI_INFO_TYPE_OBJECT:
+ if (G_VALUE_HOLDS_PARAM (value))
+ arg->v_pointer = g_value_get_param (value);
+ else
+ arg->v_pointer = g_value_get_object (value);
+ return TRUE;
+
+ case GI_INFO_TYPE_BOXED:
+ case GI_INFO_TYPE_STRUCT:
+ case GI_INFO_TYPE_UNION:
+ if (G_VALUE_HOLDS (value, G_TYPE_BOXED))
+ arg->v_pointer = g_value_get_boxed (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_VARIANT))
+ arg->v_pointer = g_value_get_variant (value);
+ else if (G_VALUE_HOLDS (value, G_TYPE_POINTER))
+ arg->v_pointer = g_value_get_pointer (value);
+ else
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_IMPLEMENTED,
+ "Converting GValue's of type '%s' is not implemented.",
+ g_type_name (G_VALUE_TYPE (value)));
+ return FALSE;
+ }
+ return TRUE;
+
+ case GI_INFO_TYPE_INVALID:
+ case GI_INFO_TYPE_INVALID_0:
+ case GI_INFO_TYPE_FUNCTION:
+ case GI_INFO_TYPE_CONSTANT:
+ case GI_INFO_TYPE_CALLBACK:
+ case GI_INFO_TYPE_VALUE:
+ case GI_INFO_TYPE_SIGNAL:
+ case GI_INFO_TYPE_VFUNC:
+ case GI_INFO_TYPE_PROPERTY:
+ case GI_INFO_TYPE_FIELD:
+ case GI_INFO_TYPE_ARG:
+ case GI_INFO_TYPE_TYPE:
+ case GI_INFO_TYPE_UNRESOLVED:
+ default:
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_IMPLEMENTED,
+ "Converting GValue's of type '%s' is not implemented.",
+ g_info_type_to_string (info_type));
+ return FALSE;
+ }
+
+ g_assert_not_reached ();
+ }
+
+ case GI_TYPE_TAG_ERROR:
+ if (G_VALUE_HOLDS (value, G_TYPE_ERROR))
+ arg->v_pointer = g_value_get_boxed (value);
+ else
+ return_type_mismatch (value, G_TYPE_ERROR);
+ return TRUE;
+
+ case GI_TYPE_TAG_VOID:
+ if (G_VALUE_HOLDS (value, G_TYPE_POINTER))
+ arg->v_pointer = g_value_get_pointer (value);
+ else
+ return_type_mismatch (value, G_TYPE_POINTER);
+ return TRUE;
+
+ default:
+ break;
+ }
+
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_NOT_IMPLEMENTED,
+ "Unknown marshaling error.");
+
+ return FALSE;
+}
+
+gboolean
+tmpl_gi_argument_to_g_value (GValue *value,
+ GITypeInfo *type_info,
+ GIArgument *arg,
+ GError **error)
+{
+ GITypeTag tag;
+
+ g_assert (value != NULL);
+ g_assert (type_info != NULL);
+ g_assert (arg != NULL);
+
+ tag = g_type_info_get_tag (type_info);
+
+ if (tag == GI_TYPE_TAG_INTERFACE)
+ {
+ g_warning ("TODO: proper return marshaling");
+ }
+
+ switch (tag)
+ {
+ case GI_TYPE_TAG_VOID:
+ /* No type info */
+ return TRUE;
+
+ case GI_TYPE_TAG_BOOLEAN:
+ g_value_init (value, G_TYPE_BOOLEAN);
+ g_value_set_boolean (value, arg->v_boolean);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT8:
+ g_value_init (value, G_TYPE_INT);
+ g_value_set_int (value, arg->v_int8);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT16:
+ g_value_init (value, G_TYPE_INT);
+ g_value_set_int (value, arg->v_int16);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT32:
+ g_value_init (value, G_TYPE_INT);
+ g_value_set_int (value, arg->v_int32);
+ return TRUE;
+
+ case GI_TYPE_TAG_INT64:
+ g_value_init (value, G_TYPE_INT64);
+ g_value_set_int64 (value, arg->v_int64);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT8:
+ g_value_init (value, G_TYPE_UINT);
+ g_value_set_uint (value, arg->v_uint8);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT16:
+ g_value_init (value, G_TYPE_UINT);
+ g_value_set_uint (value, arg->v_uint16);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT32:
+ g_value_init (value, G_TYPE_UINT);
+ g_value_set_uint (value, arg->v_uint32);
+ return TRUE;
+
+ case GI_TYPE_TAG_UINT64:
+ g_value_init (value, G_TYPE_UINT64);
+ g_value_set_uint64 (value, arg->v_uint64);
+ return TRUE;
+
+ case GI_TYPE_TAG_FLOAT:
+ g_value_init (value, G_TYPE_FLOAT);
+ g_value_set_float (value, arg->v_float);
+ return TRUE;
+
+ case GI_TYPE_TAG_DOUBLE:
+ g_value_init (value, G_TYPE_DOUBLE);
+ g_value_set_float (value, arg->v_double);
+ return TRUE;
+
+ case GI_TYPE_TAG_GTYPE:
+ g_value_init (value, G_TYPE_GTYPE);
+ g_value_set_gtype (value, arg->v_long);
+ return TRUE;
+
+ case GI_TYPE_TAG_UTF8:
+ case GI_TYPE_TAG_FILENAME:
+ g_value_init (value, G_TYPE_STRING);
+ g_value_set_string (value, arg->v_string);
+ return TRUE;
+
+ case GI_TYPE_TAG_ARRAY:
+ case GI_TYPE_TAG_INTERFACE:
+ case GI_TYPE_TAG_GLIST:
+ case GI_TYPE_TAG_GSLIST:
+ case GI_TYPE_TAG_GHASH:
+ case GI_TYPE_TAG_ERROR:
+ break;
+
+ case GI_TYPE_TAG_UNICHAR:
+ {
+ gchar str[8];
+
+ str [g_unichar_to_utf8 (arg->v_int32, str)] = '\0';
+ g_value_init (value, G_TYPE_STRING);
+ g_value_set_string (value, str);
+
+ return TRUE;
+ }
+
+ default:
+ break;
+ }
+
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_TYPE_MISMATCH,
+ "Failed to decode value from GObject Introspection");
+
+ return FALSE;
+}
diff --git a/contrib/tmpl/tmpl-glib.h b/contrib/tmpl/tmpl-glib.h
new file mode 100644
index 0000000..ee8a608
--- /dev/null
+++ b/contrib/tmpl/tmpl-glib.h
@@ -0,0 +1,38 @@
+/* tmpl-glib.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_GLIB_H
+#define TMPL_GLIB_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define TMPL_GLIB_INSIDE
+# include "tmpl-error.h"
+# include "tmpl-expr.h"
+# include "tmpl-expr-types.h"
+# include "tmpl-scope.h"
+# include "tmpl-symbol.h"
+# include "tmpl-template.h"
+# include "tmpl-template-locator.h"
+#undef TMPL_GLIB_INSIDE
+
+G_END_DECLS
+
+#endif /* TMPL_GLIB_H */
diff --git a/contrib/tmpl/tmpl-iter-node.c b/contrib/tmpl/tmpl-iter-node.c
new file mode 100644
index 0000000..e302f9e
--- /dev/null
+++ b/contrib/tmpl/tmpl-iter-node.c
@@ -0,0 +1,189 @@
+/* tmpl-iter-node.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-iter-node"
+
+#include "tmpl-debug.h"
+#include "tmpl-error.h"
+#include "tmpl-iter-node.h"
+
+struct _TmplIterNode
+{
+ TmplNode parent_instance;
+
+ gchar *identifier;
+ TmplExpr *expr;
+ GPtrArray *children;
+};
+
+G_DEFINE_TYPE (TmplIterNode, tmpl_iter_node, TMPL_TYPE_NODE)
+
+static gboolean
+tmpl_iter_node_accept (TmplNode *node,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplIterNode *self = (TmplIterNode *)node;
+
+ TMPL_ENTRY;
+
+ g_assert (TMPL_IS_ITER_NODE (self));
+ g_assert (lexer != NULL);
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ while (TRUE)
+ {
+ TmplToken *token;
+ TmplNode *child;
+
+ if (!tmpl_lexer_next (lexer, &token, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ switch (tmpl_token_type (token))
+ {
+ case TMPL_TOKEN_EOF:
+ tmpl_token_free (token);
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Unexpectedly reached end of file");
+ TMPL_RETURN (FALSE);
+
+ case TMPL_TOKEN_END:
+ tmpl_token_free (token);
+ TMPL_RETURN (TRUE);
+
+ case TMPL_TOKEN_TEXT:
+ case TMPL_TOKEN_IF:
+ case TMPL_TOKEN_ELSE:
+ case TMPL_TOKEN_ELSE_IF:
+ case TMPL_TOKEN_FOR:
+ case TMPL_TOKEN_EXPRESSION:
+ case TMPL_TOKEN_INCLUDE:
+ default:
+ if (!(child = tmpl_node_new_for_token (token, error)))
+ {
+ tmpl_token_free (token);
+ TMPL_RETURN (FALSE);
+ }
+
+ g_ptr_array_add (self->children, child);
+ tmpl_token_free (token);
+
+ if (!tmpl_node_accept (child, lexer, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ break;
+ }
+ }
+
+ g_assert_not_reached ();
+}
+
+static void
+tmpl_iter_node_visit_children (TmplNode *node,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ TmplIterNode *self = (TmplIterNode *)node;
+ gint i;
+
+ g_assert (TMPL_IS_ITER_NODE (self));
+ g_assert (visitor != NULL);
+
+ for (i = 0; i < self->children->len; i++)
+ {
+ TmplNode *child = g_ptr_array_index (self->children, i);
+
+ visitor (child, user_data);
+ }
+}
+
+static void
+tmpl_iter_node_finalize (GObject *object)
+{
+ TmplIterNode *self = (TmplIterNode *)object;
+
+ g_clear_pointer (&self->identifier, g_free);
+ g_clear_pointer (&self->expr, tmpl_expr_unref);
+ g_clear_pointer (&self->children, g_ptr_array_unref);
+
+ G_OBJECT_CLASS (tmpl_iter_node_parent_class)->finalize (object);
+}
+
+static void
+tmpl_iter_node_class_init (TmplIterNodeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ TmplNodeClass *node_class = TMPL_NODE_CLASS (klass);
+
+ object_class->finalize = tmpl_iter_node_finalize;
+
+ node_class->accept = tmpl_iter_node_accept;
+ node_class->visit_children = tmpl_iter_node_visit_children;
+}
+
+static void
+tmpl_iter_node_init (TmplIterNode *self)
+{
+ self->children = g_ptr_array_new_with_free_func (g_object_unref);
+}
+
+/**
+ * tmpl_iter_node_new:
+ * @identifier: the name of the variable inside the loop.
+ * @expr: (transfer full): A #TmplExpr.
+ *
+ * Returns: (transfer full): A #TmplIterNode.
+ */
+TmplNode *
+tmpl_iter_node_new (const gchar *identifier,
+ TmplExpr *expr)
+{
+ TmplIterNode *self;
+
+ g_return_val_if_fail (expr != NULL, NULL);
+
+ self = g_object_new (TMPL_TYPE_ITER_NODE, NULL);
+ self->identifier = g_strdup (identifier);
+ self->expr = expr;
+
+ return TMPL_NODE (self);
+}
+
+/**
+ * tmpl_iter_node_get_expr:
+ *
+ * Returns: (transfer none): An #TmplExpr.
+ */
+TmplExpr *
+tmpl_iter_node_get_expr (TmplIterNode *self)
+{
+ g_return_val_if_fail (TMPL_IS_ITER_NODE (self), NULL);
+
+ return self->expr;
+}
+
+const gchar *
+tmpl_iter_node_get_identifier (TmplIterNode *self)
+{
+ g_return_val_if_fail (TMPL_IS_ITER_NODE (self), NULL);
+
+ return self->identifier;
+}
diff --git a/contrib/tmpl/tmpl-iter-node.h b/contrib/tmpl/tmpl-iter-node.h
new file mode 100644
index 0000000..3762292
--- /dev/null
+++ b/contrib/tmpl/tmpl-iter-node.h
@@ -0,0 +1,42 @@
+/* tmpl-iter-node.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_ITER_NODE_H
+#define TMPL_ITER_NODE_H
+
+#include "tmpl-expr.h"
+#include "tmpl-node.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_ITER_NODE (tmpl_iter_node_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplIterNode, tmpl_iter_node, TMPL, ITER_NODE, TmplNode)
+
+TmplNode *tmpl_iter_node_new (const gchar *identifier,
+ TmplExpr *expr);
+TmplExpr *tmpl_iter_node_get_expr (TmplIterNode *self);
+const gchar *tmpl_iter_node_get_identifier (TmplIterNode *self);
+
+G_END_DECLS
+
+#endif /* TMPL_ITER_NODE_H */
diff --git a/contrib/tmpl/tmpl-iterator.c b/contrib/tmpl/tmpl-iterator.c
new file mode 100644
index 0000000..1446d68
--- /dev/null
+++ b/contrib/tmpl/tmpl-iterator.c
@@ -0,0 +1,142 @@
+/* tmpl-iterator.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gio/gio.h>
+#include <string.h>
+
+#include "tmpl-iterator.h"
+
+typedef gboolean (*GetValue) (TmplIterator *iter,
+ GValue *value);
+typedef gboolean (*MoveNext) (TmplIterator *iter);
+typedef void (*Destroy) (TmplIterator *iter);
+
+static gboolean
+string_move_next (TmplIterator *iter)
+{
+ if (iter->instance)
+ {
+ iter->instance = g_utf8_next_char ((gchar *)iter->instance);
+ return (*(gchar *)iter->instance) != 0;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+string_get_value (TmplIterator *iter,
+ GValue *value)
+{
+ if (iter->instance)
+ {
+ gunichar ch = g_utf8_get_char ((gchar *)iter->instance);
+ gchar str[8];
+
+ str [g_unichar_to_utf8 (ch, str)] = '\0';
+ g_value_init (value, G_TYPE_STRING);
+ g_value_set_string (value, str);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+list_model_move_next (TmplIterator *iter)
+{
+ guint index = GPOINTER_TO_INT (iter->data1);
+ guint n_items = GPOINTER_TO_INT (iter->data2);
+
+ if (++index < n_items)
+ {
+ iter->data1 = GINT_TO_POINTER (index);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+list_model_get_value (TmplIterator *iter,
+ GValue *value)
+{
+ guint index = GPOINTER_TO_INT (iter->data1);
+ GObject *obj;
+
+ obj = g_list_model_get_item (iter->instance, index);
+
+ if (obj != NULL)
+ g_value_init (value, G_OBJECT_TYPE (obj));
+ else
+ g_value_init (value, G_TYPE_OBJECT);
+
+ g_value_take_object (value, obj);
+
+ return TRUE;
+}
+
+void
+tmpl_iterator_init (TmplIterator *iter,
+ const GValue *value)
+{
+ memset (iter, 0, sizeof *iter);
+
+ if (value == NULL)
+ return;
+
+ if (G_VALUE_HOLDS_STRING (value))
+ {
+ iter->instance = (gchar *)g_value_get_string (value);
+ iter->move_next = string_move_next;
+ iter->get_value = string_get_value;
+ iter->destroy = NULL;
+ }
+ else if (G_VALUE_HOLDS (value, G_TYPE_LIST_MODEL))
+ {
+ iter->instance = g_value_get_object (value);
+ iter->move_next = list_model_move_next;
+ iter->get_value = list_model_get_value;
+ iter->destroy = NULL;
+ }
+ /* TODO: More iter types */
+}
+
+gboolean
+tmpl_iterator_next (TmplIterator *iter)
+{
+ if (iter == NULL || iter->move_next == NULL)
+ return FALSE;
+
+ return ((MoveNext)iter->move_next) (iter);
+}
+
+void
+tmpl_iterator_get_value (TmplIterator *iter,
+ GValue *value)
+{
+ ((GetValue)iter->get_value) (iter, value);
+}
+
+void
+tmpl_iterator_destroy (TmplIterator *iter)
+{
+ if (iter->destroy != NULL)
+ ((Destroy)(iter->destroy)) (iter);
+ memset (iter, 0, sizeof *iter);
+}
diff --git a/contrib/tmpl/tmpl-iterator.h b/contrib/tmpl/tmpl-iterator.h
new file mode 100644
index 0000000..2a97848
--- /dev/null
+++ b/contrib/tmpl/tmpl-iterator.h
@@ -0,0 +1,54 @@
+/* tmpl-iterator.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_ITERATOR_H
+#define TMPL_ITERATOR_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef struct _TmplIterator TmplIterator;
+
+struct _TmplIterator
+{
+ /*< private >*/
+ gpointer instance; /* Data */
+ gpointer move_next; /* MoveNext */
+ gpointer get_value; /* GetValue */
+ gpointer destroy; /* Destroy */
+ gpointer data1;
+ gpointer data2;
+ gpointer data3;
+ gpointer data4;
+};
+
+void tmpl_iterator_init (TmplIterator *self,
+ const GValue *value);
+gboolean tmpl_iterator_next (TmplIterator *self);
+void tmpl_iterator_get_value (TmplIterator *self,
+ GValue *value);
+void tmpl_iterator_destroy (TmplIterator *self);
+
+G_END_DECLS
+
+#endif /* TMPL_ITERATOR_H */
diff --git a/contrib/tmpl/tmpl-lexer.c b/contrib/tmpl/tmpl-lexer.c
new file mode 100644
index 0000000..fbf7d66
--- /dev/null
+++ b/contrib/tmpl/tmpl-lexer.c
@@ -0,0 +1,199 @@
+/* tmpl-lexer.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-lexer"
+
+#include "tmpl-error.h"
+#include "tmpl-debug.h"
+#include "tmpl-lexer.h"
+#include "tmpl-template-locator.h"
+#include "tmpl-token-input-stream.h"
+
+struct _TmplLexer
+{
+ GQueue *stream_stack;
+ TmplTemplateLocator *locator;
+ GHashTable *circular;
+ GSList *unget;
+};
+
+G_DEFINE_POINTER_TYPE (TmplLexer, tmpl_lexer)
+
+TmplLexer *
+tmpl_lexer_new (GInputStream *stream,
+ TmplTemplateLocator *locator)
+{
+ TmplLexer *self;
+
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+ g_return_val_if_fail (!locator || TMPL_IS_TEMPLATE_LOCATOR (locator), NULL);
+
+ self = g_slice_new0 (TmplLexer);
+ self->stream_stack = g_queue_new ();
+ self->locator = locator ? g_object_ref (locator) : tmpl_template_locator_new ();
+ self->circular = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+ g_queue_push_head (self->stream_stack, tmpl_token_input_stream_new (stream));
+
+ return self;
+}
+
+void
+tmpl_lexer_free (TmplLexer *self)
+{
+ if (self != NULL)
+ {
+ const GList *iter;
+
+ for (iter = self->stream_stack->head; iter != NULL; iter = iter->next)
+ {
+ TmplTokenInputStream *stream = iter->data;
+
+ g_object_unref (stream);
+ }
+
+ g_clear_pointer (&self->circular, g_hash_table_unref);
+ g_clear_pointer (&self->stream_stack, g_queue_free);
+ g_clear_object (&self->locator);
+ g_slice_free (TmplLexer, self);
+ }
+}
+
+/**
+ * tmpl_lexer_next:
+ * @self: A #TmplLexer.
+ * @token: (out) (transfer full): A location for a #TmplToken.
+ * @cancellable: (nullable): A #GCancellable or %NULL.
+ * @error: A location for a #GError or %NULL.
+ *
+ * Reads the next token.
+ *
+ * It is possible for %FALSE to be returned and @error left unset.
+ * This happens at the end of the stream.
+ *
+ * Returns: %TRUE if @token was set, otherwise %FALSE.
+ */
+gboolean
+tmpl_lexer_next (TmplLexer *self,
+ TmplToken **token,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplTokenInputStream *stream;
+ GError *local_error = NULL;
+ gboolean ret = FALSE;
+
+ TMPL_ENTRY;
+
+ g_return_val_if_fail (self != NULL, FALSE);
+ g_return_val_if_fail (token != NULL, FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ *token = NULL;
+
+ if (self->unget != NULL)
+ {
+ *token = self->unget->data;
+ self->unget = g_slist_remove_link (self->unget, self->unget);
+ TMPL_RETURN (TRUE);
+ }
+
+ while ((stream = g_queue_peek_head (self->stream_stack)))
+ {
+ if (!(*token = tmpl_token_input_stream_read_token (stream, cancellable, &local_error)))
+ {
+ /*
+ * If we finished this stream, try to move to the next one.
+ */
+ if (local_error == NULL)
+ {
+ g_queue_pop_head (self->stream_stack);
+ g_object_unref (stream);
+ continue;
+ }
+
+ TMPL_GOTO (finish);
+ }
+
+ /*
+ * If the current token is an include token, we need to resolve the
+ * include path and read tokens from it.
+ */
+ if (tmpl_token_type (*token) == TMPL_TOKEN_INCLUDE)
+ {
+ const gchar *path = tmpl_token_include_get_path (*token);
+ GInputStream *input;
+
+ g_assert (self->circular != NULL);
+ g_assert (path != NULL);
+
+ if (g_hash_table_contains (self->circular, path))
+ {
+ local_error = g_error_new (TMPL_ERROR,
+ TMPL_ERROR_CIRCULAR_INCLUDE,
+ "A circular include was detected: \"%s\"",
+ path);
+ g_clear_pointer (token, tmpl_token_free);
+ TMPL_GOTO (finish);
+ }
+
+ if (!(input = tmpl_template_locator_locate (self->locator, path, &local_error)))
+ {
+ g_clear_pointer (token, tmpl_token_free);
+ TMPL_GOTO (finish);
+ }
+
+ g_hash_table_insert (self->circular, g_strdup (path), NULL);
+
+ stream = tmpl_token_input_stream_new (input);
+ g_queue_push_head (self->stream_stack, stream);
+
+ g_clear_pointer (token, tmpl_token_free);
+ g_object_unref (input);
+
+ continue;
+ }
+
+ ret = TRUE;
+ break;
+ }
+
+ if (*token == NULL)
+ {
+ *token = tmpl_token_new_eof ();
+ ret = TRUE;
+ }
+
+finish:
+ if ((ret == FALSE) && (local_error != NULL))
+ g_propagate_error (error, local_error);
+
+ g_assert (ret == FALSE || *token != NULL);
+
+ TMPL_RETURN (ret);
+}
+
+void
+tmpl_lexer_unget (TmplLexer *self,
+ TmplToken *token)
+{
+ g_return_if_fail (self != NULL);
+ g_return_if_fail (token != NULL);
+
+ self->unget = g_slist_prepend (self->unget, token);
+}
diff --git a/contrib/tmpl/tmpl-lexer.h b/contrib/tmpl/tmpl-lexer.h
new file mode 100644
index 0000000..7270777
--- /dev/null
+++ b/contrib/tmpl/tmpl-lexer.h
@@ -0,0 +1,49 @@
+/* tmpl-lexer.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_LEXER_H
+#define TMPL_LEXER_H
+
+#include <gio/gio.h>
+
+#include "tmpl-token.h"
+#include "tmpl-template-locator.h"
+
+G_BEGIN_DECLS
+
+typedef struct _TmplLexer TmplLexer;
+
+GType tmpl_lexer_get_type (void);
+TmplLexer *tmpl_lexer_new (GInputStream *stream,
+ TmplTemplateLocator *locator);
+void tmpl_lexer_free (TmplLexer *self);
+void tmpl_lexer_unget (TmplLexer *self,
+ TmplToken *token);
+gboolean tmpl_lexer_next (TmplLexer *self,
+ TmplToken **token,
+ GCancellable *cancellable,
+ GError **error);
+
+
+G_END_DECLS
+
+#endif /* TMPL_LEXER_H */
diff --git a/contrib/tmpl/tmpl-node.c b/contrib/tmpl/tmpl-node.c
new file mode 100644
index 0000000..b6a490a
--- /dev/null
+++ b/contrib/tmpl/tmpl-node.c
@@ -0,0 +1,339 @@
+/* tmpl-node.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-node"
+
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "tmpl-branch-node.h"
+#include "tmpl-debug.h"
+#include "tmpl-error.h"
+#include "tmpl-expr-node.h"
+#include "tmpl-iter-node.h"
+#include "tmpl-node.h"
+#include "tmpl-parser.h"
+#include "tmpl-text-node.h"
+
+typedef struct
+{
+ GPtrArray *children;
+} TmplNodePrivate;
+
+typedef struct
+{
+ GString *str;
+ gint depth;
+} TmplNodePrintf;
+
+static void tmpl_node_printf_string (TmplNode *self,
+ GString *str,
+ gint depth);
+
+G_DEFINE_TYPE_WITH_PRIVATE (TmplNode, tmpl_node, G_TYPE_OBJECT)
+
+static gboolean
+tmpl_node_real_accept (TmplNode *self,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplNodePrivate *priv = tmpl_node_get_instance_private (self);
+ TmplToken *token = NULL;
+ TmplNode *child;
+
+ TMPL_ENTRY;
+
+ g_assert (TMPL_IS_NODE (self));
+ g_assert (lexer != NULL);
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ while (TRUE)
+ {
+ if (!tmpl_lexer_next (lexer, &token, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ switch (tmpl_token_type (token))
+ {
+ case TMPL_TOKEN_TEXT:
+ case TMPL_TOKEN_EXPRESSION:
+ case TMPL_TOKEN_IF:
+ case TMPL_TOKEN_FOR:
+ if (!(child = tmpl_node_new_for_token (token, error)))
+ {
+ tmpl_token_free (token);
+ TMPL_RETURN (FALSE);
+ }
+
+ tmpl_token_free (token);
+
+ if (priv->children == NULL)
+ priv->children = g_ptr_array_new_with_free_func (g_object_unref);
+ g_ptr_array_add (priv->children, child);
+
+ if (!tmpl_node_accept (child, lexer, cancellable, error))
+ TMPL_RETURN (FALSE);
+
+ break;
+
+ case TMPL_TOKEN_EOF:
+ TMPL_RETURN (TRUE);
+
+ case TMPL_TOKEN_ELSE_IF:
+ case TMPL_TOKEN_ELSE:
+ case TMPL_TOKEN_END:
+ case TMPL_TOKEN_INCLUDE:
+ default:
+ tmpl_token_free (token);
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Received invalid token from lexer");
+ TMPL_RETURN (FALSE);
+ }
+ }
+
+ g_assert_not_reached ();
+}
+
+static void
+tmpl_node_real_visit_children (TmplNode *self,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ TmplNodePrivate *priv = tmpl_node_get_instance_private (self);
+ gint i;
+
+ TMPL_ENTRY;
+
+ g_assert (TMPL_IS_NODE (self));
+ g_assert (visitor != NULL);
+
+ if (priv->children != NULL)
+ {
+ for (i = 0; i < priv->children->len; i++)
+ {
+ TmplNode *child = g_ptr_array_index (priv->children, i);
+
+ visitor (child, user_data);
+ }
+ }
+
+ TMPL_EXIT;
+}
+
+static void
+tmpl_node_finalize (GObject *object)
+{
+ TmplNode *self = (TmplNode *)object;
+ TmplNodePrivate *priv = tmpl_node_get_instance_private (self);
+
+ g_clear_pointer (&priv->children, g_ptr_array_unref);
+
+ G_OBJECT_CLASS (tmpl_node_parent_class)->finalize (object);
+}
+
+static void
+tmpl_node_class_init (TmplNodeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = tmpl_node_finalize;
+
+ klass->accept = tmpl_node_real_accept;
+ klass->visit_children = tmpl_node_real_visit_children;
+}
+
+static void
+tmpl_node_init (TmplNode *self)
+{
+}
+
+TmplNode *
+tmpl_node_new (void)
+{
+ return g_object_new (TMPL_TYPE_NODE, NULL);
+}
+
+gboolean
+tmpl_node_accept (TmplNode *self,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ g_return_val_if_fail (TMPL_IS_NODE (self), FALSE);
+ g_return_val_if_fail (lexer != NULL, FALSE);
+
+ return TMPL_NODE_GET_CLASS (self)->accept (self, lexer, cancellable, error);
+}
+
+void
+tmpl_node_visit_children (TmplNode *self,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ g_return_if_fail (TMPL_IS_NODE (self));
+ g_return_if_fail (visitor != NULL);
+
+ return TMPL_NODE_GET_CLASS (self)->visit_children (self, visitor, user_data);
+}
+
+TmplNode *
+tmpl_node_new_for_token (TmplToken *token,
+ GError **error)
+{
+ TmplNode *ret;
+
+ TMPL_ENTRY;
+
+ g_return_val_if_fail (token != NULL, NULL);
+
+ switch (tmpl_token_type (token))
+ {
+ case TMPL_TOKEN_TEXT:
+ ret = tmpl_text_node_new (g_strdup (tmpl_token_get_text (token)));
+ TMPL_RETURN (ret);
+
+ case TMPL_TOKEN_IF:
+ {
+ TmplExpr *expr;
+ const gchar *exprstr;
+
+ exprstr = tmpl_token_get_text (token);
+
+ if (!(expr = tmpl_expr_from_string (exprstr, error)))
+ TMPL_RETURN (NULL);
+
+ ret = tmpl_branch_node_new (expr);
+ TMPL_RETURN (ret);
+ }
+
+ case TMPL_TOKEN_FOR:
+ {
+ const gchar *item_in_expr;
+ TmplExpr *expr;
+ TmplNode *node = NULL;
+ char *item = NULL;
+ char *exprstr = NULL;
+
+ if (!(item_in_expr = tmpl_token_get_text (token)))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Invalid for expression");
+ TMPL_RETURN (NULL);
+ }
+
+ if (2 != sscanf (item_in_expr, "%ms in %ms", &item, &exprstr))
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_SYNTAX_ERROR,
+ "Invalid for expression: %s", item_in_expr);
+ goto for_cleanup;
+ }
+
+ if (!(expr = tmpl_expr_from_string (exprstr, error)))
+ goto for_cleanup;
+
+ node = tmpl_iter_node_new (item, expr);
+
+ for_cleanup:
+ free (item);
+ free (exprstr);
+
+ return node;
+ }
+
+ case TMPL_TOKEN_EXPRESSION:
+ {
+ TmplExpr *expr;
+ const gchar *exprstr;
+
+ exprstr = tmpl_token_get_text (token);
+
+ if (!(expr = tmpl_expr_from_string (exprstr, error)))
+ TMPL_RETURN (NULL);
+
+ ret = tmpl_expr_node_new (expr);
+ TMPL_RETURN (ret);
+ }
+
+ case TMPL_TOKEN_ELSE_IF:
+ case TMPL_TOKEN_ELSE:
+ case TMPL_TOKEN_END:
+ case TMPL_TOKEN_INCLUDE:
+ case TMPL_TOKEN_EOF:
+ default:
+ g_assert_not_reached ();
+ TMPL_RETURN (NULL);
+ }
+}
+
+static void
+tmpl_node_printf_visitor (TmplNode *node,
+ gpointer user_data)
+{
+ TmplNodePrintf *state = user_data;
+
+ g_assert (TMPL_IS_NODE (node));
+ g_assert (state != NULL);
+ g_assert (state->str != NULL);
+ g_assert (state->depth > 0);
+
+ tmpl_node_printf_string (node, state->str, state->depth);
+}
+
+static void
+tmpl_node_printf_string (TmplNode *self,
+ GString *str,
+ gint depth)
+{
+ TmplNodePrintf state = { str, depth + 1 };
+ gint i;
+
+ g_assert (TMPL_IS_NODE (self));
+ g_assert (str != NULL);
+
+ for (i = 0; i < depth; i++)
+ g_string_append (str, " ");
+ g_string_append (str, G_OBJECT_TYPE_NAME (self));
+ g_string_append_c (str, '\n');
+
+ tmpl_node_visit_children (self,
+ tmpl_node_printf_visitor,
+ &state);
+}
+
+gchar *
+tmpl_node_printf (TmplNode *self)
+{
+ GString *str;
+
+ g_return_val_if_fail (TMPL_IS_NODE (self), NULL);
+
+ str = g_string_new (NULL);
+ tmpl_node_printf_string (self, str, 0);
+
+ return g_string_free (str, FALSE);
+}
diff --git a/contrib/tmpl/tmpl-node.h b/contrib/tmpl/tmpl-node.h
new file mode 100644
index 0000000..59a4222
--- /dev/null
+++ b/contrib/tmpl/tmpl-node.h
@@ -0,0 +1,66 @@
+/* tmpl-node.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_NODE_H
+#define TMPL_NODE_H
+
+#include <gio/gio.h>
+
+#include "tmpl-lexer.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_NODE (tmpl_node_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (TmplNode, tmpl_node, TMPL, NODE, GObject)
+
+typedef void (*TmplNodeVisitor) (TmplNode *self,
+ gpointer user_data);
+
+struct _TmplNodeClass
+{
+ GObjectClass parent_class;
+
+ gboolean (*accept) (TmplNode *self,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error);
+ void (*visit_children) (TmplNode *self,
+ TmplNodeVisitor visitor,
+ gpointer user_data);
+};
+
+TmplNode *tmpl_node_new (void);
+TmplNode *tmpl_node_new_for_token (TmplToken *token,
+ GError **error);
+gboolean tmpl_node_accept (TmplNode *self,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error);
+gchar *tmpl_node_printf (TmplNode *self);
+void tmpl_node_visit_children (TmplNode *self,
+ TmplNodeVisitor visitor,
+ gpointer user_data);
+
+G_END_DECLS
+
+#endif /* TMPL_NODE_H */
diff --git a/contrib/tmpl/tmpl-parser.c b/contrib/tmpl/tmpl-parser.c
new file mode 100644
index 0000000..a106845
--- /dev/null
+++ b/contrib/tmpl/tmpl-parser.c
@@ -0,0 +1,267 @@
+/* tmpl-parser.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "tmpl-error.h"
+#include "tmpl-lexer.h"
+#include "tmpl-node.h"
+#include "tmpl-parser.h"
+
+struct _TmplParser
+{
+ GObject parent_instance;
+
+ TmplNode *root;
+ GInputStream *stream;
+ TmplTemplateLocator *locator;
+
+ guint has_parsed : 1;
+};
+
+enum {
+ PROP_0,
+ PROP_LOCATOR,
+ PROP_STREAM,
+ LAST_PROP
+};
+
+G_DEFINE_TYPE (TmplParser, tmpl_parser, G_TYPE_OBJECT)
+
+static GParamSpec *properties [LAST_PROP];
+
+static void
+tmpl_parser_set_stream (TmplParser *self,
+ GInputStream *stream)
+{
+ g_assert (TMPL_IS_PARSER (self));
+ g_assert (!stream || G_IS_INPUT_STREAM (stream));
+
+ if (stream == NULL)
+ {
+ g_warning ("TmplParser created without a stream!");
+ return;
+ }
+
+ g_set_object (&self->stream, stream);
+}
+
+static void
+tmpl_parser_finalize (GObject *object)
+{
+ TmplParser *self = (TmplParser *)object;
+
+ g_clear_object (&self->locator);
+ g_clear_object (&self->stream);
+ g_clear_object (&self->root);
+
+ G_OBJECT_CLASS (tmpl_parser_parent_class)->finalize (object);
+}
+
+static void
+tmpl_parser_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ TmplParser *self = TMPL_PARSER(object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCATOR:
+ g_value_set_object (value, tmpl_parser_get_locator (self));
+ break;
+
+ case PROP_STREAM:
+ g_value_set_object (value, self->stream);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
+tmpl_parser_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ TmplParser *self = TMPL_PARSER(object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCATOR:
+ tmpl_parser_set_locator (self, g_value_get_object (value));
+ break;
+
+ case PROP_STREAM:
+ tmpl_parser_set_stream (self, g_value_get_object (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
+tmpl_parser_class_init (TmplParserClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = tmpl_parser_finalize;
+ object_class->get_property = tmpl_parser_get_property;
+ object_class->set_property = tmpl_parser_set_property;
+
+ properties [PROP_LOCATOR] =
+ g_param_spec_object ("locator",
+ "Locator",
+ "The template locator for resolving includes",
+ TMPL_TYPE_TEMPLATE_LOCATOR,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ properties [PROP_STREAM] =
+ g_param_spec_object ("stream",
+ "Stream",
+ "The stream to parse",
+ G_TYPE_INPUT_STREAM,
+ (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, LAST_PROP, properties);
+}
+
+static void
+tmpl_parser_init (TmplParser *self)
+{
+ self->root = tmpl_node_new ();
+}
+
+TmplParser *
+tmpl_parser_new (GInputStream *stream)
+{
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+
+ return g_object_new (TMPL_TYPE_PARSER,
+ "stream", stream,
+ NULL);
+}
+
+/**
+ * tmpl_parser_get_root:
+ * @self: A #TmplNode.
+ *
+ * Gets the root node for the parser.
+ *
+ * See tmpl_parser_visit_children() to apply a visitor to all nodes created
+ * by the parser.
+ *
+ * Returns: (transfer none): An #TmplNode.
+ */
+TmplNode *
+tmpl_parser_get_root (TmplParser *self)
+{
+ g_return_val_if_fail (TMPL_IS_PARSER (self), NULL);
+
+ return self->root;
+}
+
+gboolean
+tmpl_parser_parse (TmplParser *self,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplLexer *lexer;
+ GError *local_error = NULL;
+
+ g_return_val_if_fail (TMPL_IS_PARSER (self), FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ if (self->has_parsed)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_INVALID_STATE,
+ _("%s() may only be called once"),
+ G_STRFUNC);
+ return FALSE;
+ }
+
+ self->has_parsed = TRUE;
+
+ if (self->stream == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_INVALID_STATE,
+ _("Parser does not contain an input stream"));
+ return FALSE;
+ }
+
+ lexer = tmpl_lexer_new (self->stream, self->locator);
+ tmpl_node_accept (self->root, lexer, cancellable, &local_error);
+ tmpl_lexer_free (lexer);
+
+ if (local_error != NULL)
+ {
+ g_propagate_error (error, local_error);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/**
+ * tmpl_parser_get_locator:
+ * @self: an #TmplParser
+ *
+ * Gets the template loader used for resolving includes when parsing template
+ * files.
+ *
+ * Includes are performed using the {{include "path"}} token. The locator can
+ * be used to restrict where the templates are located from. By default, the
+ * search path is empty, and includes cannot be performed.
+ *
+ * Returns: (transfer none): A #TmplTemplateLocator.
+ */
+TmplTemplateLocator *
+tmpl_parser_get_locator (TmplParser *self)
+{
+ g_return_val_if_fail (TMPL_IS_PARSER (self), NULL);
+
+ return self->locator;
+}
+
+/**
+ * tmpl_parser_set_locator:
+ * @self: A #TmplParser
+ * @locator: A #TmplTemplateLocator
+ *
+ * Sets the template locator used to resolve {{include "path"}} directives.
+ *
+ * See tmpl_parser_get_locator() for more information.
+ */
+void
+tmpl_parser_set_locator (TmplParser *self,
+ TmplTemplateLocator *locator)
+{
+ g_return_if_fail (TMPL_IS_PARSER (self));
+ g_return_if_fail (!locator || TMPL_IS_TEMPLATE_LOCATOR (locator));
+
+ if (g_set_object (&self->locator, locator))
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LOCATOR]);
+}
diff --git a/contrib/tmpl/tmpl-parser.h b/contrib/tmpl/tmpl-parser.h
new file mode 100644
index 0000000..94395db
--- /dev/null
+++ b/contrib/tmpl/tmpl-parser.h
@@ -0,0 +1,48 @@
+/* tmpl-parser.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_PARSER_H
+#define TMPL_PARSER_H
+
+#include <gio/gio.h>
+
+#include "tmpl-node.h"
+#include "tmpl-template-locator.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_PARSER (tmpl_parser_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplParser, tmpl_parser, TMPL, PARSER, GObject)
+
+TmplNode *tmpl_parser_get_root (TmplParser *self);
+TmplParser *tmpl_parser_new (GInputStream *stream);
+TmplTemplateLocator *tmpl_parser_get_locator (TmplParser *self);
+void tmpl_parser_set_locator (TmplParser *self,
+ TmplTemplateLocator *locator);
+gboolean tmpl_parser_parse (TmplParser *self,
+ GCancellable *cancellable,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* TMPL_PARSER_H */
diff --git a/contrib/tmpl/tmpl-scope.c b/contrib/tmpl/tmpl-scope.c
new file mode 100644
index 0000000..f1c892a
--- /dev/null
+++ b/contrib/tmpl/tmpl-scope.c
@@ -0,0 +1,168 @@
+/* tmpl-scope.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tmpl-scope.h"
+#include "tmpl-symbol.h"
+
+struct _TmplScope
+{
+ volatile gint ref_count;
+ TmplScope *parent;
+ GHashTable *symbols;
+};
+
+G_DEFINE_BOXED_TYPE (TmplScope, tmpl_scope, tmpl_scope_ref, tmpl_scope_unref)
+
+TmplScope *
+tmpl_scope_ref (TmplScope *self)
+{
+ g_return_val_if_fail (self != NULL, NULL);
+ g_return_val_if_fail (self->ref_count > 0, NULL);
+
+ g_atomic_int_inc (&self->ref_count);
+
+ return self;
+}
+
+void
+tmpl_scope_unref (TmplScope *self)
+{
+ g_return_if_fail (self != NULL);
+ g_return_if_fail (self->ref_count > 0);
+
+ if (g_atomic_int_dec_and_test (&self->ref_count))
+ {
+ g_clear_pointer (&self->symbols, g_hash_table_unref);
+ g_clear_pointer (&self->parent, tmpl_scope_unref);
+ g_slice_free (TmplScope, self);
+ }
+}
+
+/**
+ * tmpl_scope_new:
+ *
+ * Creates a new scope to contain variables and custom expressions,
+ *
+ * Returns: (transfer full): A newly created #TmplScope.
+ */
+TmplScope *
+tmpl_scope_new (void)
+{
+ TmplScope *self;
+
+ self = g_slice_new0 (TmplScope);
+ self->ref_count = 1;
+ self->parent = NULL;
+
+ return self;
+}
+
+/**
+ * tmpl_scope_new_with_parent:
+ * @parent: (nullable): An optional parent scope
+ *
+ * Creates a new scope to contain variables and custom expressions,
+ * If @parent is set, the parent scope will be inherited.
+ *
+ * Returns: (transfer full): A newly created #TmplScope.
+ */
+TmplScope *
+tmpl_scope_new_with_parent (TmplScope *parent)
+{
+ TmplScope *self;
+
+ self = g_slice_new0 (TmplScope);
+ self->ref_count = 1;
+ self->parent = parent != NULL ? tmpl_scope_ref (parent) : NULL;
+
+ return self;
+}
+
+static TmplSymbol *
+tmpl_scope_get_full (TmplScope *self,
+ const gchar *name,
+ gboolean create)
+{
+ TmplSymbol *symbol = NULL;
+
+ g_return_val_if_fail (self != NULL, NULL);
+
+ /* See if this scope has the symbol */
+ if (self->symbols != NULL)
+ {
+ if ((symbol = g_hash_table_lookup (self->symbols, name)))
+ return symbol;
+ }
+
+ /* Try to locate the symbol in a parent scope */
+ if (symbol == NULL)
+ {
+ TmplScope *parent;
+
+ for (parent = self->parent; parent != NULL; parent = parent->parent)
+ {
+ if (parent->symbols != NULL)
+ {
+ if ((symbol = g_hash_table_lookup (parent->symbols, name)))
+ return symbol;
+ }
+ }
+ }
+
+ if (create)
+ {
+ /* Define the symbol in this scope */
+ symbol = tmpl_symbol_new ();
+ if (self->symbols == NULL)
+ self->symbols = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ g_free,
+ (GDestroyNotify)tmpl_symbol_unref);
+ g_hash_table_insert (self->symbols, g_strdup (name), symbol);
+ }
+
+ return symbol;
+}
+
+/**
+ * tmpl_scope_get:
+ *
+ * If the symbol could not be found, it will be allocated.
+ *
+ * Returns: (transfer none): A #TmplSymbol.
+ */
+TmplSymbol *
+tmpl_scope_get (TmplScope *self,
+ const gchar *name)
+{
+ return tmpl_scope_get_full (self, name, TRUE);
+}
+
+/**
+ * tmpl_scope_peek:
+ *
+ * If the symbol could not be found, %NULL is returned.
+ *
+ * Returns: (transfer none) (nullable): A #TmplSymbol or %NULL.
+ */
+TmplSymbol *
+tmpl_scope_peek (TmplScope *self,
+ const gchar *name)
+{
+ return tmpl_scope_get_full (self, name, FALSE);
+}
diff --git a/contrib/tmpl/tmpl-scope.h b/contrib/tmpl/tmpl-scope.h
new file mode 100644
index 0000000..60efd9a
--- /dev/null
+++ b/contrib/tmpl/tmpl-scope.h
@@ -0,0 +1,46 @@
+/* tmpl-scope.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_SCOPE_H
+#define TMPL_SCOPE_H
+
+#include "tmpl-expr-types.h"
+
+G_BEGIN_DECLS
+
+TmplScope *tmpl_scope_new (void);
+TmplScope *tmpl_scope_new_with_parent (TmplScope *parent);
+TmplScope *tmpl_scope_ref (TmplScope *self);
+void tmpl_scope_unref (TmplScope *self);
+TmplSymbol *tmpl_scope_peek (TmplScope *self,
+ const gchar *name);
+TmplSymbol *tmpl_scope_get (TmplScope *self,
+ const gchar *name);
+void tmpl_scope_set (TmplScope *self,
+ const gchar *name,
+ TmplSymbol *symbol);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (TmplScope, tmpl_scope_unref)
+
+G_END_DECLS
+
+#endif /* TMPL_SCOPE_H */
diff --git a/contrib/tmpl/tmpl-symbol.c b/contrib/tmpl/tmpl-symbol.c
new file mode 100644
index 0000000..78339c7
--- /dev/null
+++ b/contrib/tmpl/tmpl-symbol.c
@@ -0,0 +1,237 @@
+/* tmpl-symbol.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tmpl-expr.h"
+#include "tmpl-symbol.h"
+
+G_DEFINE_BOXED_TYPE (TmplSymbol, tmpl_symbol, tmpl_symbol_ref, tmpl_symbol_unref)
+
+struct _TmplSymbol
+{
+ volatile gint ref_count;
+ TmplSymbolType type;
+ union {
+ GValue value;
+ struct {
+ TmplExpr *expr;
+ GPtrArray *params;
+ } expr;
+ } u;
+};
+
+TmplSymbol *
+tmpl_symbol_new (void)
+{
+ TmplSymbol *self;
+
+ self = g_slice_new0 (TmplSymbol);
+ self->ref_count = 1;
+ self->type = TMPL_SYMBOL_VALUE;
+
+ return self;
+}
+
+static inline void
+tmpl_symbol_clear (TmplSymbol *self)
+{
+ if ((self->type == TMPL_SYMBOL_VALUE) &&
+ (G_VALUE_TYPE (&self->u.value) != G_TYPE_INVALID))
+ g_value_unset (&self->u.value);
+ else if (self->type == TMPL_SYMBOL_EXPR)
+ {
+ g_clear_pointer (&self->u.expr.expr, tmpl_expr_unref);
+ g_clear_pointer (&self->u.expr.params, g_ptr_array_unref);
+ }
+}
+
+TmplSymbol *
+tmpl_symbol_ref (TmplSymbol *self)
+{
+ g_return_val_if_fail (self != NULL, NULL);
+ g_return_val_if_fail (self->ref_count > 0, NULL);
+
+ g_atomic_int_inc (&self->ref_count);
+
+ return self;
+}
+
+void
+tmpl_symbol_unref (TmplSymbol *self)
+{
+ g_return_if_fail (self != NULL);
+ g_return_if_fail (self->ref_count > 0);
+
+ if (g_atomic_int_dec_and_test (&self->ref_count))
+ {
+ tmpl_symbol_clear (self);
+ g_slice_free (TmplSymbol, self);
+ }
+}
+
+void
+tmpl_symbol_assign_value (TmplSymbol *self,
+ const GValue *value)
+{
+ g_return_if_fail (self != NULL);
+
+ tmpl_symbol_clear (self);
+
+ self->type = TMPL_SYMBOL_VALUE;
+
+ if ((value != NULL) && (G_VALUE_TYPE (value) != G_TYPE_INVALID))
+ {
+ g_value_init (&self->u.value, G_VALUE_TYPE (value));
+ g_value_copy (value, &self->u.value);
+ }
+}
+
+/**
+ * tmpl_symbol_assign_expr: (skip)
+ * @self: A #TmplSymbol.
+ * @expr: (nullable): An expression to assign, or %NULL.
+ * params: (element-type utf8): A #GPtrArray of strings.
+ *
+ * Sets the symbol as a %TMPL_SYMBOL_EXPR with the given ordered and
+ * named parameters.
+ */
+void
+tmpl_symbol_assign_expr (TmplSymbol *self,
+ TmplExpr *expr,
+ GPtrArray *params)
+{
+ g_return_if_fail (self != NULL);
+
+ tmpl_symbol_clear (self);
+
+ self->type = TMPL_SYMBOL_EXPR;
+
+ if (expr != NULL)
+ self->u.expr.expr = tmpl_expr_ref (expr);
+
+ if (params != NULL)
+ self->u.expr.params = g_ptr_array_ref (params);
+}
+
+TmplSymbolType
+tmpl_symbol_get_symbol_type (TmplSymbol *self)
+{
+ g_return_val_if_fail (self != NULL, 0);
+
+ return self->type;
+}
+
+/**
+ * tmpl_symbol_get_expr:
+ * @self: A #TmplSymbol
+ * @params: (out) (element-type utf8) (transfer none) (nullable): A list of parameters
+ *
+ * Returns: (transfer none): A #TmplExpr.
+ */
+TmplExpr *
+tmpl_symbol_get_expr (TmplSymbol *self,
+ GPtrArray **params)
+{
+ g_return_val_if_fail (self != NULL, 0);
+
+ if (self->type != TMPL_SYMBOL_EXPR)
+ {
+ g_warning ("Attempt to fetch TmplExpr from a value symbol");
+ return NULL;
+ }
+
+ if (params != NULL)
+ *params = self->u.expr.params;
+
+ return self->u.expr.expr;
+}
+
+void
+tmpl_symbol_get_value (TmplSymbol *self,
+ GValue *value)
+{
+ g_return_if_fail (self != NULL);
+ g_return_if_fail (value != NULL);
+
+ if (self->type != TMPL_SYMBOL_VALUE)
+ {
+ g_warning ("Attempt to fetch value from a expr symbol");
+ return;
+ }
+
+ if (G_VALUE_TYPE (&self->u.value) != G_TYPE_INVALID)
+ {
+ g_value_init (value, G_VALUE_TYPE (&self->u.value));
+ g_value_copy (&self->u.value, value);
+ }
+}
+
+void
+tmpl_symbol_assign_boolean (TmplSymbol *self,
+ gboolean v_bool)
+{
+ GValue value = G_VALUE_INIT;
+
+ g_return_if_fail (self != NULL);
+
+ g_value_init (&value, G_TYPE_BOOLEAN);
+ g_value_set_boolean (&value, v_bool);
+ tmpl_symbol_assign_value (self, &value);
+ g_value_unset (&value);
+}
+
+void
+tmpl_symbol_assign_double (TmplSymbol *self,
+ gdouble v_double)
+{
+ GValue value = G_VALUE_INIT;
+
+ g_return_if_fail (self != NULL);
+
+ g_value_init (&value, G_TYPE_DOUBLE);
+ g_value_set_double (&value, v_double);
+ tmpl_symbol_assign_value (self, &value);
+ g_value_unset (&value);
+}
+
+void
+tmpl_symbol_assign_string (TmplSymbol *self,
+ const gchar *v_string)
+{
+ GValue value = G_VALUE_INIT;
+
+ g_return_if_fail (self != NULL);
+
+ g_value_init (&value, G_TYPE_STRING);
+ g_value_set_string (&value, v_string);
+ tmpl_symbol_assign_value (self, &value);
+ g_value_unset (&value);
+}
+
+void
+tmpl_symbol_assign_object (TmplSymbol *self,
+ gpointer v_object)
+{
+ GValue value = G_VALUE_INIT;
+
+ g_return_if_fail (self != NULL);
+
+ g_value_init (&value, G_TYPE_OBJECT);
+ g_value_set_object (&value, v_object);
+ tmpl_symbol_assign_value (self, &value);
+ g_value_unset (&value);
+}
diff --git a/contrib/tmpl/tmpl-symbol.h b/contrib/tmpl/tmpl-symbol.h
new file mode 100644
index 0000000..cb21d0f
--- /dev/null
+++ b/contrib/tmpl/tmpl-symbol.h
@@ -0,0 +1,54 @@
+/* tmpl-symbol.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_SYMBOL_H
+#define TMPL_SYMBOL_H
+
+#include "tmpl-expr-types.h"
+
+G_BEGIN_DECLS
+
+TmplSymbol *tmpl_symbol_new (void);
+TmplSymbol *tmpl_symbol_ref (TmplSymbol *self);
+void tmpl_symbol_unref (TmplSymbol *self);
+TmplSymbolType tmpl_symbol_get_symbol_type (TmplSymbol *self);
+void tmpl_symbol_get_value (TmplSymbol *self,
+ GValue *value);
+TmplExpr *tmpl_symbol_get_expr (TmplSymbol *self,
+ GPtrArray **params);
+void tmpl_symbol_assign_value (TmplSymbol *self,
+ const GValue *value);
+void tmpl_symbol_assign_boolean (TmplSymbol *self,
+ gboolean v_bool);
+void tmpl_symbol_assign_double (TmplSymbol *self,
+ gdouble v_double);
+void tmpl_symbol_assign_string (TmplSymbol *self,
+ const gchar *v_string);
+void tmpl_symbol_assign_object (TmplSymbol *self,
+ gpointer v_object);
+void tmpl_symbol_assign_expr (TmplSymbol *self,
+ TmplExpr *expr,
+ GPtrArray *args);
+
+G_END_DECLS
+
+#endif /* TMPL_SYMBOL_H */
diff --git a/contrib/tmpl/tmpl-template-locator.c b/contrib/tmpl/tmpl-template-locator.c
new file mode 100644
index 0000000..8fc6b5a
--- /dev/null
+++ b/contrib/tmpl/tmpl-template-locator.c
@@ -0,0 +1,207 @@
+/* tmpl-template-locator.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "tmpl-error.h"
+#include "tmpl-template-locator.h"
+
+typedef struct
+{
+ GQueue *search_path;
+} TmplTemplateLocatorPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (TmplTemplateLocator, tmpl_template_locator, G_TYPE_OBJECT)
+
+static GInputStream *
+tmpl_template_locator_locate_in_path (TmplTemplateLocator *self,
+ const gchar *path_base,
+ const gchar *path)
+{
+ GInputStream *ret = NULL;
+ gchar *full_path;
+
+ g_assert (TMPL_IS_TEMPLATE_LOCATOR (self));
+ g_assert (path_base != NULL);
+ g_assert (path != NULL);
+
+ full_path = g_build_path (path_base, path, NULL);
+
+ if (g_str_has_prefix (full_path, "resource://"))
+ {
+ /*
+ * A mediocre attempt to prevent escapes using ../
+ */
+ if (strstr (full_path, "..") == NULL)
+ ret = g_resources_open_stream (full_path + strlen ("resource://"), 0, NULL);
+ }
+ else
+ {
+ GFile *parent = g_file_new_for_path (path_base);
+ GFile *file = g_file_new_for_path (full_path);
+ gchar *relative;
+
+ /*
+ * If the path tries to escape the search path, using ../../ or
+ * something clever, we will get an invalid path here.
+ */
+ if ((relative = g_file_get_relative_path (parent, file)))
+ {
+ g_free (relative);
+ ret = (GInputStream *)g_file_read (file, NULL, NULL);
+ }
+
+ g_object_unref (parent);
+ g_object_unref (file);
+ }
+
+ g_free (full_path);
+
+ return ret;
+}
+
+static GInputStream *
+tmpl_template_locator_real_locate (TmplTemplateLocator *self,
+ const gchar *path,
+ GError **error)
+{
+ TmplTemplateLocatorPrivate *priv = tmpl_template_locator_get_instance_private (self);
+ GInputStream *ret = NULL;
+ const GList *iter;
+ const GList *search_path;
+
+ g_assert (TMPL_IS_TEMPLATE_LOCATOR (self));
+ g_assert (path != NULL);
+
+ search_path = priv->search_path->head;
+
+ for (iter = search_path; ret == NULL && iter != NULL; iter = iter->next)
+ {
+ const gchar *path_base = iter->data;
+
+ ret = tmpl_template_locator_locate_in_path (self, path_base, path);
+ }
+
+ if (ret == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_TEMPLATE_NOT_FOUND,
+ _("Failed to locate template \"%s\""),
+ path);
+ return NULL;
+ }
+
+ return ret;
+}
+
+static void
+tmpl_template_locator_class_init (TmplTemplateLocatorClass *klass)
+{
+ klass->locate = tmpl_template_locator_real_locate;
+}
+
+static void
+tmpl_template_locator_init (TmplTemplateLocator *self)
+{
+ TmplTemplateLocatorPrivate *priv = tmpl_template_locator_get_instance_private (self);
+
+ priv->search_path = g_queue_new ();
+}
+
+void
+tmpl_template_locator_append_search_path (TmplTemplateLocator *self,
+ const gchar *path)
+{
+ TmplTemplateLocatorPrivate *priv = tmpl_template_locator_get_instance_private (self);
+
+ g_return_if_fail (TMPL_IS_TEMPLATE_LOCATOR (self));
+ g_return_if_fail (path != NULL);
+
+ g_queue_push_tail (priv->search_path, g_strdup (path));
+}
+
+void
+tmpl_template_locator_prepend_search_path (TmplTemplateLocator *self,
+ const gchar *path)
+{
+ TmplTemplateLocatorPrivate *priv = tmpl_template_locator_get_instance_private (self);
+
+ g_return_if_fail (TMPL_IS_TEMPLATE_LOCATOR (self));
+ g_return_if_fail (path != NULL);
+
+ g_queue_push_head (priv->search_path, g_strdup (path));
+}
+
+/**
+ * tmpl_template_locator_get_search_path:
+ * @self: A #TmplTemplateLocator
+ *
+ * Gets the current search path used by the template locator.
+ *
+ * Returns: (transfer full): A %NULL-terminated array of strings.
+ */
+gchar **
+tmpl_template_locator_get_search_path (TmplTemplateLocator *self)
+{
+ TmplTemplateLocatorPrivate *priv = tmpl_template_locator_get_instance_private (self);
+ GPtrArray *ar;
+ const GList *iter;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE_LOCATOR (self), NULL);
+
+ ar = g_ptr_array_new ();
+
+ for (iter = priv->search_path->head; iter != NULL; iter = iter->next)
+ {
+ const gchar *path = iter->data;
+
+ g_ptr_array_add (ar, g_strdup (path));
+ }
+
+ g_ptr_array_add (ar, NULL);
+
+ return (gchar **)g_ptr_array_free (ar, FALSE);
+}
+
+TmplTemplateLocator *
+tmpl_template_locator_new (void)
+{
+ return g_object_new (TMPL_TYPE_TEMPLATE_LOCATOR, NULL);
+}
+
+/**
+ * tmpl_template_locator_locate:
+ * @self: A #TmplTemplateLocator.
+ * @path: a relative path to the file
+ *
+ * This will resolve the relative path using the search paths found within
+ * the template loader.
+ *
+ * Returns: (transfer full): A #GInputStream or %NULL and @error is set.
+ */
+GInputStream *
+tmpl_template_locator_locate (TmplTemplateLocator *self,
+ const gchar *path,
+ GError **error)
+{
+ g_return_val_if_fail (TMPL_IS_TEMPLATE_LOCATOR (self), NULL);
+ g_return_val_if_fail (path != NULL, NULL);
+
+ return TMPL_TEMPLATE_LOCATOR_GET_CLASS (self)->locate (self, path, error);
+}
diff --git a/contrib/tmpl/tmpl-template-locator.h b/contrib/tmpl/tmpl-template-locator.h
new file mode 100644
index 0000000..9afd85e
--- /dev/null
+++ b/contrib/tmpl/tmpl-template-locator.h
@@ -0,0 +1,57 @@
+/* tmpl-template-locator.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_TEMPLATE_LOCATOR_H
+#define TMPL_TEMPLATE_LOCATOR_H
+
+#include <gio/gio.h>
+
+#include "tmpl-template-locator.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_TEMPLATE_LOCATOR (tmpl_template_locator_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (TmplTemplateLocator, tmpl_template_locator, TMPL, TEMPLATE_LOCATOR, GObject)
+
+struct _TmplTemplateLocatorClass
+{
+ GObjectClass parent_instance;
+
+ GInputStream *(*locate) (TmplTemplateLocator *self,
+ const gchar *path,
+ GError **error);
+};
+
+TmplTemplateLocator *tmpl_template_locator_new (void);
+void tmpl_template_locator_append_search_path (TmplTemplateLocator *self,
+ const gchar *path);
+void tmpl_template_locator_prepend_search_path (TmplTemplateLocator *self,
+ const gchar *path);
+GInputStream *tmpl_template_locator_locate (TmplTemplateLocator *self,
+ const gchar *path,
+ GError **error);
+gchar **tmpl_template_locator_get_search_path (TmplTemplateLocator *self);
+
+G_END_DECLS
+
+#endif /* TMPL_TEMPLATE_LOCATOR_H */
diff --git a/contrib/tmpl/tmpl-template.c b/contrib/tmpl/tmpl-template.c
new file mode 100644
index 0000000..cedaf03
--- /dev/null
+++ b/contrib/tmpl/tmpl-template.c
@@ -0,0 +1,540 @@
+/* tmpl-template.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-template"
+
+#include <glib/gi18n.h>
+#include <string.h>
+
+#include "tmpl-branch-node.h"
+#include "tmpl-condition-node.h"
+#include "tmpl-error.h"
+#include "tmpl-expr-node.h"
+#include "tmpl-iter-node.h"
+#include "tmpl-iterator.h"
+#include "tmpl-parser.h"
+#include "tmpl-scope.h"
+#include "tmpl-symbol.h"
+#include "tmpl-template.h"
+#include "tmpl-text-node.h"
+#include "tmpl-util-private.h"
+
+typedef struct
+{
+ TmplParser *parser;
+ TmplTemplateLocator *locator;
+} TmplTemplatePrivate;
+
+typedef struct
+{
+ TmplTemplate *self;
+ TmplNode *root;
+ GString *output;
+ TmplScope *scope;
+ GError **error;
+ gboolean result;
+} TmplTemplateExpandState;
+
+G_DEFINE_TYPE_WITH_PRIVATE (TmplTemplate, tmpl_template, G_TYPE_OBJECT)
+
+enum {
+ PROP_0,
+ PROP_LOCATOR,
+ LAST_PROP
+};
+
+static GParamSpec *properties [LAST_PROP];
+
+static void
+tmpl_template_finalize (GObject *object)
+{
+ TmplTemplate *self = (TmplTemplate *)object;
+ TmplTemplatePrivate *priv = tmpl_template_get_instance_private (self);
+
+ g_clear_object (&priv->parser);
+
+ G_OBJECT_CLASS (tmpl_template_parent_class)->finalize (object);
+}
+
+static void
+tmpl_template_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ TmplTemplate *self = TMPL_TEMPLATE(object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCATOR:
+ g_value_set_object (value, tmpl_template_get_locator (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
+tmpl_template_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ TmplTemplate *self = TMPL_TEMPLATE(object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCATOR:
+ tmpl_template_set_locator (self, g_value_get_object (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
+tmpl_template_class_init (TmplTemplateClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = tmpl_template_finalize;
+ object_class->get_property = tmpl_template_get_property;
+ object_class->set_property = tmpl_template_set_property;
+
+ properties [PROP_LOCATOR] =
+ g_param_spec_object ("locator",
+ "Locator",
+ "The locator used for resolving includes",
+ TMPL_TYPE_TEMPLATE_LOCATOR,
+ (G_PARAM_READWRITE |
+ G_PARAM_EXPLICIT_NOTIFY |
+ G_PARAM_CONSTRUCT |
+ G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, LAST_PROP, properties);
+}
+
+static void
+tmpl_template_init (TmplTemplate *self)
+{
+}
+
+TmplTemplate *
+tmpl_template_new (TmplTemplateLocator *locator)
+{
+ return g_object_new (TMPL_TYPE_TEMPLATE,
+ "locator", locator,
+ NULL);
+}
+
+gboolean
+tmpl_template_parse_file (TmplTemplate *self,
+ GFile *file,
+ GCancellable *cancellable,
+ GError **error)
+{
+ GInputStream *stream;
+ gboolean ret = FALSE;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (G_IS_FILE (file), FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ stream = (GInputStream *)g_file_read (file, cancellable, error);
+
+ if (stream != NULL)
+ {
+ ret = tmpl_template_parse (self, stream, cancellable, error);
+ g_object_unref (stream);
+ }
+
+ return ret;
+}
+
+gboolean
+tmpl_template_parse_path (TmplTemplate *self,
+ const gchar *path,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gboolean ret;
+ GFile *file;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (path != NULL, FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ file = g_file_new_for_path (path);
+ ret = tmpl_template_parse_file (self, file, cancellable, error);
+ g_object_unref (file);
+
+ return ret;
+}
+
+gboolean
+tmpl_template_parse_resource (TmplTemplate *self,
+ const gchar *resource_path,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gchar *copied = NULL;
+ gboolean ret;
+ GFile *file;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (resource_path != NULL, FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ if (!g_str_has_prefix (resource_path, "resource://"))
+ resource_path = copied = g_strdup_printf ("resource://%s", resource_path);
+
+ file = g_file_new_for_uri (resource_path);
+ ret = tmpl_template_parse_file (self, file, cancellable, error);
+
+ g_object_unref (file);
+ g_free (copied);
+
+ return ret;
+}
+
+gboolean
+tmpl_template_parse_string (TmplTemplate *self,
+ const gchar *str,
+ GError **error)
+{
+ GInputStream *stream;
+ gboolean ret;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (str, FALSE);
+
+ stream = g_memory_input_stream_new_from_data (g_strdup (str), strlen (str), g_free);
+ ret = tmpl_template_parse (self, stream, NULL, error);
+ g_object_unref (stream);
+
+ return ret;
+}
+
+gboolean
+tmpl_template_parse (TmplTemplate *self,
+ GInputStream *stream,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplTemplatePrivate *priv = tmpl_template_get_instance_private (self);
+ TmplParser *parser;
+ gboolean ret = FALSE;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ parser = tmpl_parser_new (stream);
+
+ tmpl_parser_set_locator (parser, priv->locator);
+
+ if (tmpl_parser_parse (parser, cancellable, error))
+ {
+ g_set_object (&priv->parser, parser);
+ ret = TRUE;
+ }
+
+ g_object_unref (parser);
+
+ return ret;
+}
+
+static void
+value_into_string (const GValue *value,
+ GString *str)
+{
+ GValue transform = G_VALUE_INIT;
+
+ g_value_init (&transform, G_TYPE_STRING);
+
+ if (g_value_transform (value, &transform))
+ {
+ const gchar *tmp;
+
+ if (NULL != (tmp = g_value_get_string (&transform)))
+ g_string_append (str, tmp);
+ }
+
+ g_value_unset (&transform);
+}
+
+static void
+tmpl_template_expand_visitor (TmplNode *node,
+ gpointer user_data)
+{
+ TmplTemplateExpandState *state = user_data;
+
+ g_assert (TMPL_IS_NODE (node));
+ g_assert (state != NULL);
+
+/* Short cirtcuit if an error occurred */
+ if (state->result == FALSE)
+ return;
+
+ if (TMPL_IS_TEXT_NODE (node))
+ {
+ g_string_append (state->output, tmpl_text_node_get_text (TMPL_TEXT_NODE (node)));
+ }
+ else if (TMPL_IS_EXPR_NODE (node))
+ {
+ GValue return_value = { 0 };
+ TmplExpr *expr;
+
+ expr = tmpl_expr_node_get_expr (TMPL_EXPR_NODE (node));
+
+ if (!tmpl_expr_eval (expr, state->scope, &return_value, state->error))
+ {
+ state->result = FALSE;
+ return;
+ }
+
+ value_into_string (&return_value, state->output);
+ g_value_unset (&return_value);
+ }
+ else if (TMPL_IS_BRANCH_NODE (node))
+ {
+ TmplNode *child;
+ GError *local_error = NULL;
+
+ child = tmpl_branch_node_branch (TMPL_BRANCH_NODE (node), state->scope, &local_error);
+
+ if (child != NULL)
+ tmpl_node_visit_children (child, tmpl_template_expand_visitor, state);
+ else if (local_error != NULL)
+ {
+ g_propagate_error (state->error, local_error);
+ state->result = FALSE;
+ }
+ }
+ else if (TMPL_IS_CONDITION_NODE (node))
+ {
+ TmplExpr *expr;
+ GValue value = G_VALUE_INIT;
+
+ expr = tmpl_condition_node_get_condition (TMPL_CONDITION_NODE (node));
+
+ if (!tmpl_expr_eval (expr, state->scope, &value, state->error))
+ {
+ state->result = FALSE;
+ return;
+ }
+
+ if (tmpl_value_as_boolean (&value))
+ tmpl_node_visit_children (node, tmpl_template_expand_visitor, state);
+
+ TMPL_CLEAR_VALUE (&value);
+ }
+ else if (TMPL_IS_ITER_NODE (node))
+ {
+ const gchar *identifier;
+ TmplExpr *expr;
+ GValue return_value = G_VALUE_INIT;
+
+ identifier = tmpl_iter_node_get_identifier (TMPL_ITER_NODE (node));
+
+ expr = tmpl_iter_node_get_expr (TMPL_ITER_NODE (node));
+
+ if (!tmpl_expr_eval (expr, state->scope, &return_value, state->error))
+ {
+ state->result = FALSE;
+ return;
+ }
+
+ if (tmpl_value_as_boolean (&return_value))
+ {
+ TmplIterator iter;
+ TmplScope *old_scope = state->scope;
+ TmplScope *new_scope = tmpl_scope_new_with_parent (old_scope);
+ TmplSymbol *symbol;
+
+ state->scope = new_scope;
+
+ symbol = tmpl_scope_get (new_scope, identifier);
+
+ tmpl_iterator_init (&iter, &return_value);
+
+ while (tmpl_iterator_next (&iter))
+ {
+ GValue value = G_VALUE_INIT;
+
+ tmpl_iterator_get_value (&iter, &value);
+ tmpl_symbol_assign_value (symbol, &value);
+ TMPL_CLEAR_VALUE (&value);
+
+ tmpl_node_visit_children (node, tmpl_template_expand_visitor, state);
+ }
+
+ state->scope = old_scope;
+ tmpl_scope_unref (new_scope);
+ }
+
+ g_value_unset (&return_value);
+ }
+ else
+ {
+ g_warning ("Teach me how to expand %s", G_OBJECT_TYPE_NAME (node));
+ }
+}
+
+/**
+ * tmpl_template_expand:
+ * @self: A TmplTemplate.
+ * @stream: a #GOutputStream to write the results to
+ * @scope: (nullable): A #TmplScope containing state for the template, or %NULL.
+ * @cancellable: (nullable): An optional cancellable for the operation.
+ * @error: A location for a #GError, or %NULL.
+ *
+ * Expands a template into @stream using the @scope provided.
+ *
+ * @scope should have all of the variables set that are required to expand
+ * the template, or you will get a symbol reference error and %FALSE will
+ * be returned.
+ *
+ * To set a symbol value, get the symbol with tmpl_scope_get() and assign
+ * a value using tmpl_scope_assign_value() or similar methods.
+ *
+ * Returns: %TRUE if successful, otherwise %FALSE and @error is set.
+ */
+gboolean
+tmpl_template_expand (TmplTemplate *self,
+ GOutputStream *stream,
+ TmplScope *scope,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TmplTemplatePrivate *priv = tmpl_template_get_instance_private (self);
+ TmplTemplateExpandState state = { 0 };
+ TmplScope *local_scope = NULL;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
+ g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+ if (priv->parser == NULL)
+ {
+ g_set_error (error,
+ TMPL_ERROR,
+ TMPL_ERROR_INVALID_STATE,
+ _("Must parse template before expanding"));
+ return FALSE;
+ }
+
+ if (scope == NULL)
+ scope = local_scope = tmpl_scope_new ();
+
+ state.root = tmpl_parser_get_root (priv->parser);
+ state.self = self;
+ state.output = g_string_new (NULL);
+ state.result = TRUE;
+ state.error = error;
+ state.scope = scope;
+
+ tmpl_node_visit_children (state.root, tmpl_template_expand_visitor, &state);
+
+ if (state.result != FALSE)
+ state.result = g_output_stream_write_all (stream,
+ state.output->str,
+ state.output->len,
+ NULL,
+ cancellable,
+ error);
+
+ g_string_free (state.output, TRUE);
+
+ if (local_scope != NULL)
+ tmpl_scope_unref (local_scope);
+
+ return state.result;
+}
+
+/**
+ * tmpl_template_expand_string:
+ * @self: A #TmplTemplate.
+ * @scope: (nullable): A #TmplScope or %NULL.
+ * @error: A location for a #GError, or %NULL
+ *
+ * Expands the template and returns the result as a string.
+ *
+ * Returns: A newly allocated string, or %NULL upon failure.
+ */
+gchar *
+tmpl_template_expand_string (TmplTemplate *self,
+ TmplScope *scope,
+ GError **error)
+{
+ GOutputStream *stream;
+ gchar zero = 0;
+ gchar *ret;
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), NULL);
+
+ stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+
+ if (!tmpl_template_expand (self, stream, scope, NULL, error) ||
+ !g_output_stream_write_all (stream, &zero, 1, NULL, NULL, error) ||
+ !g_output_stream_close (stream, NULL, error))
+
+ {
+ g_object_unref (stream);
+ return NULL;
+ }
+
+ ret = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (stream));
+
+ g_object_unref (stream);
+
+ return ret;
+}
+
+/**
+ * tmpl_template_get_locator:
+ * @self: A #TmplTemplate
+ *
+ * Gets the template locator used when resolving template includes.
+ *
+ * Returns: (transfer none): a #TmplTemplateLocator or %NULL.
+ */
+TmplTemplateLocator *
+tmpl_template_get_locator (TmplTemplate *self)
+{
+ TmplTemplatePrivate *priv = tmpl_template_get_instance_private (self);
+
+ g_return_val_if_fail (TMPL_IS_TEMPLATE (self), NULL);
+
+ return priv->locator;
+}
+
+void
+tmpl_template_set_locator (TmplTemplate *self,
+ TmplTemplateLocator *locator)
+{
+ TmplTemplatePrivate *priv = tmpl_template_get_instance_private (self);
+
+ g_return_if_fail (TMPL_IS_TEMPLATE (self));
+ g_return_if_fail (!locator || TMPL_IS_TEMPLATE_LOCATOR (locator));
+
+ if (g_set_object (&priv->locator, locator))
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LOCATOR]);
+}
diff --git a/contrib/tmpl/tmpl-template.h b/contrib/tmpl/tmpl-template.h
new file mode 100644
index 0000000..95e8abc
--- /dev/null
+++ b/contrib/tmpl/tmpl-template.h
@@ -0,0 +1,76 @@
+/* tmpl-template.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_TEMPLATE_H
+#define TMPL_TEMPLATE_H
+
+#include <gio/gio.h>
+
+#include "tmpl-scope.h"
+#include "tmpl-template-locator.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_TEMPLATE (tmpl_template_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (TmplTemplate, tmpl_template, TMPL, TEMPLATE, GObject)
+
+struct _TmplTemplateClass
+{
+ GObjectClass parent_class;
+};
+
+TmplTemplate *tmpl_template_new (TmplTemplateLocator *locator);
+TmplTemplateLocator *tmpl_template_get_locator (TmplTemplate *self);
+void tmpl_template_set_locator (TmplTemplate *self,
+ TmplTemplateLocator *locator);
+gboolean tmpl_template_parse_file (TmplTemplate *self,
+ GFile *file,
+ GCancellable *cancellable,
+ GError **error);
+gboolean tmpl_template_parse_resource (TmplTemplate *self,
+ const gchar *path,
+ GCancellable *cancellable,
+ GError **error);
+gboolean tmpl_template_parse_path (TmplTemplate *self,
+ const gchar *path,
+ GCancellable *cancellable,
+ GError **error);
+gboolean tmpl_template_parse_string (TmplTemplate *self,
+ const gchar *input,
+ GError **error);
+gboolean tmpl_template_parse (TmplTemplate *self,
+ GInputStream *stream,
+ GCancellable *cancellable,
+ GError **error);
+gboolean tmpl_template_expand (TmplTemplate *self,
+ GOutputStream *stream,
+ TmplScope *scope,
+ GCancellable *cancellable,
+ GError **error);
+gchar *tmpl_template_expand_string (TmplTemplate *self,
+ TmplScope *scope,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* TMPL_TEMPLATE_H */
diff --git a/contrib/tmpl/tmpl-text-node.c b/contrib/tmpl/tmpl-text-node.c
new file mode 100644
index 0000000..470602a
--- /dev/null
+++ b/contrib/tmpl/tmpl-text-node.c
@@ -0,0 +1,105 @@
+/* tmpl-text-node.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "tmpl-text-node"
+
+#include "tmpl-debug.h"
+#include "tmpl-text-node.h"
+
+struct _TmplTextNode
+{
+ TmplNode parent_instance;
+ gchar *text;
+};
+
+G_DEFINE_TYPE (TmplTextNode, tmpl_text_node, TMPL_TYPE_NODE)
+
+static gboolean
+tmpl_text_node_accept (TmplNode *node,
+ TmplLexer *lexer,
+ GCancellable *cancellable,
+ GError **error)
+{
+ TMPL_ENTRY;
+ /* no children */
+ TMPL_RETURN (TRUE);
+}
+
+static void
+tmpl_text_node_visit_children (TmplNode *node,
+ TmplNodeVisitor visitor,
+ gpointer user_data)
+{
+ TMPL_ENTRY;
+ /* no children */
+ TMPL_EXIT;
+}
+
+static void
+tmpl_text_node_finalize (GObject *object)
+{
+ TmplTextNode *self = (TmplTextNode *)object;
+
+ g_clear_pointer (&self->text, g_free);
+
+ G_OBJECT_CLASS (tmpl_text_node_parent_class)->finalize (object);
+}
+
+static void
+tmpl_text_node_class_init (TmplTextNodeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ TmplNodeClass *node_class = TMPL_NODE_CLASS (klass);
+
+ object_class->finalize = tmpl_text_node_finalize;
+
+ node_class->accept = tmpl_text_node_accept;
+ node_class->visit_children = tmpl_text_node_visit_children;
+}
+
+static void
+tmpl_text_node_init (TmplTextNode *node)
+{
+}
+
+/**
+ * tmpl_text_node_new:
+ * @text: (transfer full): the text for the node
+ *
+ * Creates a new text node.
+ *
+ * Returns: (transfer full): the new node.
+ */
+TmplNode *
+tmpl_text_node_new (gchar *text)
+{
+ TmplTextNode *self;
+
+ self = g_object_new (TMPL_TYPE_TEXT_NODE, NULL);
+ self->text = text;
+
+ return TMPL_NODE (self);
+}
+
+const gchar *
+tmpl_text_node_get_text (TmplTextNode *self)
+{
+ g_return_val_if_fail (TMPL_IS_TEXT_NODE (self), NULL);
+
+ return self->text;
+}
diff --git a/contrib/tmpl/tmpl-text-node.h b/contrib/tmpl/tmpl-text-node.h
new file mode 100644
index 0000000..a481c9e
--- /dev/null
+++ b/contrib/tmpl/tmpl-text-node.h
@@ -0,0 +1,37 @@
+/* tmpl-text-node.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_TEXT_NODE_H
+#define TMPL_TEXT_NODE_H
+
+#include "tmpl-node.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_TEXT_NODE (tmpl_text_node_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplTextNode, tmpl_text_node, TMPL, TEXT_NODE, TmplNode)
+
+TmplNode *tmpl_text_node_new (gchar *text);
+const gchar *tmpl_text_node_get_text (TmplTextNode *self);
+void tmpl_text_node_set_text (TmplTextNode *self,
+ const gchar *text);
+
+G_END_DECLS
+
+#endif /* TMPL_TEXT_NODE_H */
diff --git a/contrib/tmpl/tmpl-token-input-stream.c b/contrib/tmpl/tmpl-token-input-stream.c
new file mode 100644
index 0000000..94d50bc
--- /dev/null
+++ b/contrib/tmpl/tmpl-token-input-stream.c
@@ -0,0 +1,307 @@
+/* tmpl-token-input-stream.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tmpl-token-input-stream.h"
+
+struct _TmplTokenInputStream
+{
+ GDataInputStream parent_instance;
+};
+
+G_DEFINE_TYPE (TmplTokenInputStream, tmpl_token_input_stream, G_TYPE_DATA_INPUT_STREAM)
+
+static void
+tmpl_token_input_stream_class_init (TmplTokenInputStreamClass *klass)
+{
+}
+
+static void
+tmpl_token_input_stream_init (TmplTokenInputStream *self)
+{
+}
+
+static gboolean
+tmpl_token_input_stream_read_unichar (TmplTokenInputStream *self,
+ gunichar *unichar,
+ GCancellable *cancellable,
+ GError **error)
+{
+ GBufferedInputStream *stream = (GBufferedInputStream *)self;
+ gchar str[8] = { 0 };
+ gint c;
+ gint n;
+ gint i;
+
+ g_assert (TMPL_IS_TOKEN_INPUT_STREAM (self));
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ if (-1 == (c = g_buffered_input_stream_read_byte (stream, cancellable, error)))
+ return FALSE;
+
+ if ((c & 0x80) == 0)
+ n = 1;
+ else if ((c & 0xE0) == 0xC0)
+ n = 2;
+ else if ((c & 0xF0) == 0xE0)
+ n = 3;
+ else if ((c & 0xF8) == 0xF0)
+ n = 4;
+ else if ((c & 0xFC) == 0xF8)
+ n = 5;
+ else if ((c & 0xFE) == 0xFC)
+ n = 6;
+ else
+ n = 0;
+
+ str [0] = c;
+
+ for (i = 1; i < n; i++)
+ {
+ if (-1 == (c = g_buffered_input_stream_read_byte (stream, cancellable, error)))
+ return FALSE;
+
+ str [i] = (gchar)c;
+ }
+
+ *unichar = g_utf8_get_char (str);
+
+ return TRUE;
+}
+
+static gchar *
+tmpl_token_input_stream_read_tag (TmplTokenInputStream *self,
+ gsize *length,
+ GCancellable *cancellable,
+ GError **error)
+{
+ GBufferedInputStream *stream = (GBufferedInputStream *)self;
+ GByteArray *ar;
+ GError *local_error = NULL;
+ gboolean in_string = FALSE;
+ guchar byte;
+ gint c;
+
+ g_assert (TMPL_IS_TOKEN_INPUT_STREAM (self));
+ g_assert (length != NULL);
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ ar = g_byte_array_new ();
+
+ while (TRUE)
+ {
+ if (-1 == (c = g_buffered_input_stream_read_byte (stream, cancellable, &local_error)))
+ goto failure;
+
+ switch (c)
+ {
+ case '\\':
+ if (in_string)
+ {
+ g_byte_array_append (ar, (const guchar *)"\\", 1);
+
+ if (-1 == (c = g_buffered_input_stream_read_byte (stream, cancellable, &local_error)))
+ goto failure;
+ }
+
+ break;
+
+ case '"':
+ in_string = !in_string;
+ break;
+
+ case '}':
+ if (!in_string)
+ {
+ if (-1 == (c = g_buffered_input_stream_read_byte (stream, cancellable, &local_error)))
+ goto failure;
+
+ /* Check if we got }} */
+ if (c == '}')
+ goto finish;
+
+ g_byte_array_append (ar, (const guchar *)"}", 1);
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ byte = (guchar)c;
+ g_byte_array_append (ar, (const guchar *)&byte, 1);
+ }
+
+finish:
+ *length = ar->len;
+
+ byte = 0;
+ g_byte_array_append (ar, (const guchar *)&byte, 1);
+
+ return (gchar *)g_byte_array_free (ar, FALSE);
+
+failure:
+ *length = 0;
+
+ g_byte_array_free (ar, TRUE);
+
+ if (local_error)
+ g_propagate_error (error, local_error);
+
+ return FALSE;
+}
+
+/**
+ * tmpl_token_input_stream_read_token:
+ * @self: An #TmplTokenInputStream
+ * @error: (nullable): An optional location for a #GError
+ *
+ * Reads the next token from the underlying stream.
+ *
+ * If there was an error, %NULL is returned and @error is set.
+ *
+ * Returns: (transfer full): A #TmplToken or %NULL.
+ */
+TmplToken *
+tmpl_token_input_stream_read_token (TmplTokenInputStream *self,
+ GCancellable *cancellable,
+ GError **error)
+{
+ GDataInputStream *stream = (GDataInputStream *)self;
+ GError *local_error = NULL;
+ gunichar ch;
+ gchar *text;
+ gsize len;
+
+ g_return_val_if_fail (TMPL_IS_TOKEN_INPUT_STREAM (self), NULL);
+
+ /*
+ * The syntax of the template language is very simple. All of our symbols
+ * start with {{ and end with }}. We use \ to escape, and you only ever
+ * need to escape the opening of {{ like \{{.
+ *
+ * We scan ahead until a { or \ and take appropriate action based upon
+ * peeking at the next char.
+ *
+ * Once we resolve that, we walk forward past the expression until }}.
+ * To walk past the expression, we need to know when we are in a
+ * string, since }} could theoretically be in there too.
+ */
+
+ text = g_data_input_stream_read_upto (stream, "\\{", -1, &len, cancellable, error);
+
+ /*
+ * Handle end of stream.
+ */
+ if (text == NULL)
+ return NULL;
+
+ /*
+ * Handle successful read up to \ or {.
+ */
+ if (*text != '\0')
+ return tmpl_token_new_text (text);
+
+ g_free (text);
+
+ /*
+ * Peek what type of delimiter we hit.
+ */
+ ch = g_data_input_stream_read_byte (stream, cancellable, &local_error);
+
+ if ((ch == 0) && (local_error != NULL))
+ {
+ g_propagate_error (error, local_error);
+ return NULL;
+ }
+
+ /*
+ * Handle possible escaped \{.
+ */
+ if (ch == '\\')
+ {
+ gchar str[8] = { 0 };
+
+ /*
+ * Get the next char after \.
+ */
+ if (!tmpl_token_input_stream_read_unichar (self, &ch, cancellable, error))
+ return tmpl_token_new_unichar ('\\');
+
+ /*
+ * Handle escaping {.
+ */
+ if (ch == '{')
+ return tmpl_token_new_unichar ('{');
+
+ /*
+ * Nothing escaped, return string as it was read.
+ */
+ g_unichar_to_utf8 (ch, str);
+
+ return tmpl_token_new_text (g_strdup_printf ("\\%s", str));
+ }
+
+ g_assert (ch == '{');
+
+ /*
+ * Look for { following {. If we reached the end of the stream, just
+ * return a token for the final {.
+ */
+ if (!tmpl_token_input_stream_read_unichar (self, &ch, cancellable, error))
+ return tmpl_token_new_unichar ('{');
+
+ /*
+ * If this is not a {{, then just return a string for the pair.
+ */
+ if (ch != '{')
+ {
+ gchar str[8] = { 0 };
+
+ g_unichar_to_utf8 (ch, str);
+
+ return tmpl_token_new_text (g_strdup_printf ("{%s", str));
+ }
+
+ /*
+ * Scan ahead until we find }}.
+ */
+ if (!(text = tmpl_token_input_stream_read_tag (self, &len, cancellable, error)))
+ return NULL;
+
+ return tmpl_token_new_generic (text);
+}
+
+/**
+ * tmpl_token_input_stream_new:
+ * @base_stream: the stream to read from
+ *
+ * Creates a #TmplTokenInputStream using @base_stream for the raw
+ * text stream.
+ *
+ * Returns: (transfer full): An #TmplTokenInputStream.
+ */
+TmplTokenInputStream *
+tmpl_token_input_stream_new (GInputStream *base_stream)
+{
+ g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
+
+ return g_object_new (TMPL_TYPE_TOKEN_INPUT_STREAM,
+ "base-stream", base_stream,
+ NULL);
+}
diff --git a/contrib/tmpl/tmpl-token-input-stream.h b/contrib/tmpl/tmpl-token-input-stream.h
new file mode 100644
index 0000000..3ebd9ce
--- /dev/null
+++ b/contrib/tmpl/tmpl-token-input-stream.h
@@ -0,0 +1,43 @@
+/* tmpl-token-input-stream.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+#ifndef TMPL_TOKEN_INPUT_STREAM_H
+#define TMPL_TOKEN_INPUT_STREAM_H
+
+#include <gio/gio.h>
+
+#include "tmpl-token.h"
+
+G_BEGIN_DECLS
+
+#define TMPL_TYPE_TOKEN_INPUT_STREAM (tmpl_token_input_stream_get_type())
+
+G_DECLARE_FINAL_TYPE (TmplTokenInputStream, tmpl_token_input_stream, TMPL, TOKEN_INPUT_STREAM,
GDataInputStream)
+
+TmplTokenInputStream *tmpl_token_input_stream_new (GInputStream *base_stream);
+TmplToken *tmpl_token_input_stream_read_token (TmplTokenInputStream *self,
+ GCancellable *cancellable,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* TMPL_TOKEN_INPUT_STREAM_H */
diff --git a/contrib/tmpl/tmpl-token.c b/contrib/tmpl/tmpl-token.c
new file mode 100644
index 0000000..a12bee8
--- /dev/null
+++ b/contrib/tmpl/tmpl-token.c
@@ -0,0 +1,181 @@
+/* tmpl-token.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "tmpl-token.h"
+
+struct _TmplToken
+{
+ TmplTokenType type;
+ gchar *text;
+};
+
+static TmplToken *
+tmpl_token_new (void)
+{
+ TmplToken *self;
+
+ self = g_slice_new0 (TmplToken);
+
+ return self;
+}
+
+void
+tmpl_token_free (TmplToken *self)
+{
+ if (self != NULL)
+ {
+ g_free (self->text);
+ g_slice_free (TmplToken, self);
+ }
+}
+
+TmplToken *
+tmpl_token_new_eof (void)
+{
+ TmplToken *ret;
+
+ ret = tmpl_token_new ();
+ ret->type = TMPL_TOKEN_EOF;
+
+ return ret;
+}
+
+/**
+ * tmpl_token_new_text:
+ * @text: (transfer full): The text for the token.
+ *
+ * Creates a new #TmplToken containing @text.
+ * This is a text literal type.
+ *
+ * Returns: (transfer full): A newly allocated #TmplToken.
+ */
+TmplToken *
+tmpl_token_new_text (gchar *text)
+{
+ TmplToken *self;
+
+ self = tmpl_token_new ();
+ self->type = TMPL_TOKEN_TEXT;
+ self->text = text;
+
+ return self;
+}
+
+TmplToken *
+tmpl_token_new_unichar (gunichar ch)
+{
+ gchar utf8[8];
+ gint len;
+
+ len = g_unichar_to_utf8 (ch, utf8);
+ utf8 [len] = '\0';
+
+ return tmpl_token_new_text (g_strdup (utf8));
+}
+
+TmplTokenType
+tmpl_token_type (TmplToken *self)
+{
+ g_return_val_if_fail (self != NULL, 0);
+
+ return self->type;
+}
+
+gchar *
+tmpl_token_include_get_path (TmplToken *self)
+{
+ char *path = NULL;
+
+ g_return_val_if_fail (self != NULL, NULL);
+ g_return_val_if_fail (self->type == TMPL_TOKEN_INCLUDE, NULL);
+
+ if (1 == sscanf (self->text, "include \"%m[^\"]", &path))
+ {
+ gchar *tmp = g_strdup (path);
+ free (path);
+ return tmp;
+ }
+
+ return NULL;
+}
+
+TmplToken *
+tmpl_token_new_generic (gchar *text)
+{
+ TmplToken *self;
+
+ g_return_val_if_fail (text != NULL, NULL);
+
+ self = g_slice_new0 (TmplToken);
+
+ if (g_str_has_prefix (text, "if "))
+ {
+ self->type = TMPL_TOKEN_IF;
+ self->text = g_strstrip (g_strdup (text + 3));
+ }
+ else if (g_str_has_prefix (text, "else if "))
+ {
+ self->type = TMPL_TOKEN_ELSE_IF;
+ self->text = g_strstrip (g_strdup (text + 8));
+ }
+ else if (g_str_has_prefix (text, "else"))
+ {
+ self->type = TMPL_TOKEN_ELSE;
+ self->text = NULL;
+ }
+ else if (g_str_has_prefix (text, "end"))
+ {
+ self->type = TMPL_TOKEN_END;
+ self->text = NULL;
+ }
+ else if (g_str_has_prefix (text, "for "))
+ {
+ self->type = TMPL_TOKEN_FOR;
+ self->text = g_strstrip (g_strdup (text + 4));
+ }
+ else if (g_str_has_prefix (text, "include "))
+ {
+ self->type = TMPL_TOKEN_INCLUDE;
+ self->text = g_strstrip (g_strdup (text));
+ }
+ else
+ {
+ self->type = TMPL_TOKEN_EXPRESSION;
+ self->text = g_strstrip (text);
+ text = NULL;
+ }
+
+ g_free (text);
+
+ return self;
+}
+
+const gchar *
+tmpl_token_get_text (TmplToken *self)
+{
+ g_return_val_if_fail (self != NULL, NULL);
+
+ return self->text;
+}
diff --git a/contrib/tmpl/tmpl-token.h b/contrib/tmpl/tmpl-token.h
new file mode 100644
index 0000000..2d370a7
--- /dev/null
+++ b/contrib/tmpl/tmpl-token.h
@@ -0,0 +1,57 @@
+/* tmpl-token.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined (TMPL_GLIB_INSIDE) && !defined (TMPL_GLIB_COMPILATION)
+# error "Only <tmpl-glib.h> can be included directly."
+#endif
+
+
+#ifndef TMPL_TOKEN_H
+#define TMPL_TOKEN_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef struct _TmplToken TmplToken;
+
+typedef enum
+{
+ TMPL_TOKEN_EOF,
+ TMPL_TOKEN_TEXT,
+ TMPL_TOKEN_IF,
+ TMPL_TOKEN_ELSE_IF,
+ TMPL_TOKEN_ELSE,
+ TMPL_TOKEN_END,
+ TMPL_TOKEN_FOR,
+ TMPL_TOKEN_EXPRESSION,
+ TMPL_TOKEN_INCLUDE,
+} TmplTokenType;
+
+TmplToken *tmpl_token_new_generic (gchar *str);
+TmplToken *tmpl_token_new_unichar (gunichar ch);
+TmplToken *tmpl_token_new_text (gchar *text);
+TmplToken *tmpl_token_new_eof (void);
+const gchar *tmpl_token_get_text (TmplToken *self);
+TmplTokenType tmpl_token_type (TmplToken *self);
+void tmpl_token_free (TmplToken *self);
+gchar *tmpl_token_include_get_path (TmplToken *self);
+
+G_END_DECLS
+
+#endif /* TMPL_TOKEN_H */
diff --git a/contrib/tmpl/tmpl-util-private.h b/contrib/tmpl/tmpl-util-private.h
new file mode 100644
index 0000000..5e33e71
--- /dev/null
+++ b/contrib/tmpl/tmpl-util-private.h
@@ -0,0 +1,36 @@
+/* tmpl-util-private.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TMPL_UTIL_PRIVATE_H
+#define TMPL_UTIL_PRIVATE_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define TMPL_CLEAR_VALUE(v) { if (G_VALUE_TYPE(v)) g_value_unset(v); }
+
+void tmpl_destroy_in_main_context (GMainContext *main_context,
+ gpointer data,
+ GDestroyNotify destroy);
+gchar *tmpl_value_repr (const GValue *value);
+gboolean tmpl_value_as_boolean (const GValue *value);
+
+G_END_DECLS
+
+#endif /* TMPL_UTIL_PRIVATE_H */
diff --git a/contrib/tmpl/tmpl-util.c b/contrib/tmpl/tmpl-util.c
new file mode 100644
index 0000000..ecfcee0
--- /dev/null
+++ b/contrib/tmpl/tmpl-util.c
@@ -0,0 +1,171 @@
+/* tmpl-util-private.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib-object.h>
+
+#include "tmpl-gi-private.h"
+#include "tmpl-util-private.h"
+
+static gboolean
+destroy_in_main_context_cb (gpointer data)
+{
+ struct {
+ gpointer data;
+ GDestroyNotify destroy;
+ } *state = data;
+
+ g_assert (state != NULL);
+ g_assert (state->data != NULL);
+ g_assert (state->destroy != NULL);
+
+ state->destroy (state->data);
+ g_slice_free1 (sizeof *state, state);
+
+ return G_SOURCE_REMOVE;
+}
+
+void
+tmpl_destroy_in_main_context (GMainContext *main_context,
+ gpointer data,
+ GDestroyNotify destroy)
+{
+ GSource *idle;
+ struct {
+ gpointer data;
+ GDestroyNotify destroy;
+ } *state;
+
+ g_assert (main_context != NULL);
+ g_assert (data != NULL);
+ g_assert (destroy != NULL);
+
+ state = g_slice_alloc (sizeof *state);
+ state->data = data;
+ state->destroy = destroy;
+
+ idle = g_idle_source_new ();
+ g_source_set_callback (idle, destroy_in_main_context_cb, state, NULL);
+ g_source_attach (idle, main_context);
+}
+
+gchar *
+tmpl_value_repr (const GValue *value)
+{
+ GValue coerced = G_VALUE_INIT;
+ gchar *ret = NULL;
+
+ g_return_val_if_fail (value != NULL, NULL);
+
+ if (G_VALUE_TYPE (value) != G_TYPE_INVALID)
+ {
+ g_value_init (&coerced, G_TYPE_STRING);
+
+ if (G_VALUE_HOLDS_BOOLEAN (value))
+ {
+ ret = g_strdup (g_value_get_boolean (value) ? "true" : "false");
+ }
+ else if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value))
+ {
+ gchar *escaped;
+
+ escaped = g_strescape (g_value_get_string (value), NULL);
+ ret = g_strdup_printf ("\"%s\"", escaped);
+ g_free (escaped);
+ }
+ else if (G_VALUE_HOLDS (value, TMPL_TYPE_TYPELIB))
+ {
+ GITypelib *tl = g_value_get_pointer (value);
+
+ if (tl != NULL)
+ {
+ const gchar *ns = g_typelib_get_namespace (tl);
+ ret = g_strdup_printf ("<Namespace \"%s\">", ns);
+ }
+ else
+ {
+ ret = g_strdup_printf ("<Namespace at %p>", tl);
+ }
+ }
+ else if (g_value_transform (value, &coerced))
+ ret = g_value_dup_string (&coerced);
+ else if (G_VALUE_HOLDS_OBJECT (value))
+ {
+ GObject *obj = g_value_get_object (value);
+
+ ret = g_strdup_printf ("<%s at %p>",
+ obj ? G_OBJECT_TYPE_NAME (obj)
+ : G_VALUE_TYPE_NAME (value),
+ obj);
+ }
+ else if (G_VALUE_HOLDS_BOXED (value))
+ ret = g_strdup_printf ("<%s at %p>",
+ G_VALUE_TYPE_NAME (value),
+ g_value_get_boxed (value));
+ else
+ ret = g_strdup_printf ("<%s>", G_VALUE_TYPE_NAME (value));
+
+ g_value_unset (&coerced);
+ }
+
+ return ret;
+}
+
+gboolean
+tmpl_value_as_boolean (const GValue *value)
+{
+ gboolean ret = FALSE;
+
+ if (value != NULL && G_VALUE_TYPE (value) != G_TYPE_INVALID)
+ {
+ GValue coerced = G_VALUE_INIT;
+
+ g_value_init (&coerced, G_TYPE_BOOLEAN);
+
+ if (!g_value_transform (value, &coerced))
+ {
+ if (G_VALUE_HOLDS_STRING (value))
+ ret = g_value_get_string (value) && *g_value_get_string (value);
+ else if (G_VALUE_HOLDS_DOUBLE (value))
+ ret = g_value_get_double (value) != 0.0;
+ else if (G_VALUE_HOLDS_INT (value))
+ ret = !!g_value_get_int (value);
+ else if (G_VALUE_HOLDS_UINT (value))
+ ret = !!g_value_get_uint (value);
+ else if (G_VALUE_HOLDS_INT64 (value))
+ ret = !!g_value_get_int64 (value);
+ else if (G_VALUE_HOLDS_UINT64 (value))
+ ret = !!g_value_get_uint64 (value);
+ else if (G_VALUE_HOLDS_LONG (value))
+ ret = !!g_value_get_long (value);
+ else if (G_VALUE_HOLDS_ULONG (value))
+ ret = !!g_value_get_ulong (value);
+ else if (G_VALUE_HOLDS_FLOAT (value))
+ ret = g_value_get_float (value) != 0.0;
+ else if (G_VALUE_HOLDS_BOXED (value))
+ ret = !!g_value_get_boxed (value);
+ else if (G_VALUE_HOLDS_OBJECT (value))
+ ret = !!g_value_get_object (value);
+ else if (G_VALUE_HOLDS_VARIANT (value))
+ ret = !!g_value_get_variant (value);
+ }
+ else
+ ret = g_value_get_boolean (&coerced);
+ }
+
+ return ret;
+}
diff --git a/data/Makefile.am b/data/Makefile.am
index 24e4bc7..118d6cd 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -12,6 +12,12 @@ servicedir = $(datadir)/dbus-1/services
service_in_files = org.gnome.Builder.service.in
service_DATA = $(service_in_files:.service.in=.service)
+# pkg-config
+pkgconfigdir = $(libdir)/gnome-builder/pkgconfig
+pkgconfig_DATA = \
+ libide-1.0.pc \
+ template-glib-1.0.pc
+
org.gnome.Builder.service: org.gnome.Builder.service.in
$(AM_V_GEN) \
[ -d $(@D) ] || $(mkdir_p) $(@D) ; \
diff --git a/data/template-glib-1.0.pc.in b/data/template-glib-1.0.pc.in
new file mode 100644
index 0000000..fa88b53
--- /dev/null
+++ b/data/template-glib-1.0.pc.in
@@ -0,0 +1,11 @@
+prefix= prefix@
+exec_prefix=${prefix}
+libdir= libdir@
+includedir= prefix@/include/gnome-builder-1.0
+
+Name: Template-GLib
+Description: Templating library for GLib
+Version: @VERSION@
+Libs: -L${libdir}/gnome-builder -ltemplate-glib-1.0
+Cflags: -I${includedir}/template-glib-1.0
+Requires: gio-2.0
diff --git a/data/theme/shared.css b/data/theme/shared.css
index 2dd8091..0b435b0 100644
--- a/data/theme/shared.css
+++ b/data/theme/shared.css
@@ -17,3 +17,28 @@ frame.gb-search-frame {
frame.gb-search-frame border {
border: none;
}
+
+/* styling for NautilusFloatingBar */
+.floating-bar {
+ padding: 2px;
+ background-color: @theme_base_color;
+ border-width: 1px;
+ border-style: solid solid none;
+ border-color: @borders;
+ border-radius: 3px 3px 0 0;
+}
+.floating-bar.bottom.left { /* axes left border and border radius */
+ border-left-style: none;
+ border-top-left-radius: 0;
+}
+.floating-bar.bottom.right { /* axes right border and border radius */
+ border-right-style: none;
+ border-top-right-radius: 0;
+}
+.floating-bar:backdrop {
+ background-color: @theme_unfocused_base_color;
+ border-color: @unfocused_borders;
+}
+.floating-bar button {
+ padding: 4px;
+}
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 8b465d5..c33352b 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -237,6 +237,12 @@ libide_1_0_la_public_sources = \
ide.h \
local/ide-local-device.c \
local/ide-local-device.h \
+ template/ide-project-template.c \
+ template/ide-project-template.h \
+ template/ide-template-base.c \
+ template/ide-template-base.h \
+ template/ide-template-provider.c \
+ template/ide-template-provider.h \
util/ide-file-manager.c \
util/ide-file-manager.h \
$(NULL)
@@ -412,6 +418,7 @@ libide_1_0_la_includes = \
-I$(top_srcdir)/contrib/nautilus \
-I$(top_srcdir)/contrib/libeditorconfig \
-I$(top_srcdir)/contrib/search \
+ -I$(top_srcdir)/contrib/tmpl \
-I$(top_srcdir)/contrib/xml \
-I$(top_builddir)/data/icons/hicolor \
-I$(srcdir) \
@@ -466,6 +473,7 @@ libide_1_0_la_LIBADD = \
$(top_builddir)/contrib/libeditorconfig/libeditorconfig.la \
$(top_builddir)/contrib/nautilus/libnautilus.la \
$(top_builddir)/contrib/search/libsearch.la \
+ $(top_builddir)/contrib/tmpl/libtemplate-glib-1.0.la \
$(top_builddir)/contrib/xml/libxml.la \
$(NULL)
@@ -528,11 +536,12 @@ introspection_sources = \
$(NULL)
Ide-1.0.gir: libide-1.0.la
-Ide_1_0_gir_INCLUDES = Gio-2.0 GtkSource-3.0 Peas-1.0
+Ide_1_0_gir_INCLUDES = Gio-2.0 GtkSource-3.0 Peas-1.0 Template-1.0
Ide_1_0_gir_CFLAGS = $(libide_1_0_la_CFLAGS)
Ide_1_0_gir_LIBS = \
libide-1.0.la \
- $(top_builddir)/contrib/egg/libegg-private.la
+ $(top_builddir)/contrib/egg/libegg-private.la \
+ $(top_builddir)/contrib/tmpl/libtemplate-glib-1.0.la
Ide_1_0_gir_FILES = $(introspection_sources)
Ide_1_0_gir_SCANNERFLAGS = \
--c-include="ide.h" \
diff --git a/libide/ide.h b/libide/ide.h
index 948fd8b..7ec2f35 100644
--- a/libide/ide.h
+++ b/libide/ide.h
@@ -126,6 +126,8 @@ G_BEGIN_DECLS
#include "doap/ide-doap.h"
#include "local/ide-local-device.h"
#include "search/ide-omni-search-row.h"
+#include "template/ide-project-template.h"
+#include "template/ide-template-provider.h"
#include "util/ide-file-manager.h"
#include "util/ide-gdk.h"
#include "util/ide-gtk.h"
diff --git a/libide/template/ide-project-template.c b/libide/template/ide-project-template.c
new file mode 100644
index 0000000..40fe8cc
--- /dev/null
+++ b/libide/template/ide-project-template.c
@@ -0,0 +1,135 @@
+/* ide-project-template.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "ide-project-template"
+
+#include "ide-project-template.h"
+
+G_DEFINE_INTERFACE (IdeProjectTemplate, ide_project_template, G_TYPE_OBJECT)
+
+static void
+ide_project_template_default_init (IdeProjectTemplateInterface *iface)
+{
+}
+
+gchar *
+ide_project_template_get_id (IdeProjectTemplate *self)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), NULL);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->get_id (self);
+}
+
+gchar *
+ide_project_template_get_name (IdeProjectTemplate *self)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), NULL);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->get_name (self);
+}
+
+gchar *
+ide_project_template_get_description (IdeProjectTemplate *self)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), NULL);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->get_description (self);
+}
+
+/**
+ * ide_project_template_get_widget:
+ * @self: An #IdeProjectTemplate
+ *
+ * Get's the configuration widget for the template if there is one.
+ *
+ * Returns: (transfer none): A #GtkWidget.
+ */
+GtkWidget *
+ide_project_template_get_widget (IdeProjectTemplate *self)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), NULL);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->get_widget (self);
+}
+
+/**
+ * ide_project_template_get_languages:
+ * @self: an #IdeProjectTemplate
+ *
+ * Gets the list of languages that this template can support when generating
+ * the project.
+ *
+ * Returns: (transfer full): A newly allocated, NULL terminated list of
+ * supported languages.
+ */
+gchar **
+ide_project_template_get_languages (IdeProjectTemplate *self)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), NULL);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->get_languages (self);
+}
+
+gchar *
+ide_project_template_get_icon_name (IdeProjectTemplate *self)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), NULL);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->get_icon_name (self);
+}
+
+/**
+ * ide_project_template_expand_async:
+ * @self: an #IdeProjectTemplate
+ * @params: (element-type utf8 GLib.Variant): A hashtable of template parameters.
+ * @cancellable: (nullable): A #GCancellable or %NULL.
+ * @callback: the callback for the asynchronous operation.
+ * @user_data: user data for @callback.
+ *
+ * Asynchronously requests expansion of the template.
+ *
+ * This may involve creating files and directories on disk as well as
+ * expanding files based on the contents of @params.
+ *
+ * It is expected that this method is only called once on an #IdeProjectTemplate.
+ */
+void
+ide_project_template_expand_async (IdeProjectTemplate *self,
+ GHashTable *params,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_return_if_fail (IDE_IS_PROJECT_TEMPLATE (self));
+ g_return_if_fail (params != NULL);
+ g_return_if_fail (g_hash_table_contains (params, "name"));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ IDE_PROJECT_TEMPLATE_GET_IFACE (self)->expand_async (self, params, cancellable, callback, user_data);
+}
+
+gboolean
+ide_project_template_expand_finish (IdeProjectTemplate *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), FALSE);
+ g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+ return IDE_PROJECT_TEMPLATE_GET_IFACE (self)->expand_finish (self, result, error);
+}
diff --git a/libide/template/ide-project-template.h b/libide/template/ide-project-template.h
new file mode 100644
index 0000000..031370d
--- /dev/null
+++ b/libide/template/ide-project-template.h
@@ -0,0 +1,67 @@
+/* ide-project-template.h
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_PROJECT_TEMPLATE_H
+#define IDE_PROJECT_TEMPLATE_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_PROJECT_TEMPLATE (ide_project_template_get_type())
+
+G_DECLARE_INTERFACE (IdeProjectTemplate, ide_project_template, IDE, PROJECT_TEMPLATE, GObject)
+
+struct _IdeProjectTemplateInterface
+{
+ GTypeInterface parent;
+
+ gchar *(*get_id) (IdeProjectTemplate *self);
+ gchar *(*get_name) (IdeProjectTemplate *self);
+ gchar *(*get_description) (IdeProjectTemplate *self);
+ GtkWidget *(*get_widget) (IdeProjectTemplate *self);
+ gchar **(*get_languages) (IdeProjectTemplate *self);
+ gchar *(*get_icon_name) (IdeProjectTemplate *self);
+ void (*expand_async) (IdeProjectTemplate *self,
+ GHashTable *params,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+ gboolean (*expand_finish) (IdeProjectTemplate *self,
+ GAsyncResult *result,
+ GError **error);
+};
+
+gchar *ide_project_template_get_id (IdeProjectTemplate *self);
+gchar *ide_project_template_get_name (IdeProjectTemplate *self);
+gchar *ide_project_template_get_description (IdeProjectTemplate *self);
+GtkWidget *ide_project_template_get_widget (IdeProjectTemplate *self);
+gchar **ide_project_template_get_languages (IdeProjectTemplate *self);
+gchar *ide_project_template_get_icon_name (IdeProjectTemplate *self);
+void ide_project_template_expand_async (IdeProjectTemplate *self,
+ GHashTable *params,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean ide_project_template_expand_finish (IdeProjectTemplate *self,
+ GAsyncResult *result,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* IDE_PROJECT_TEMPLATE_H */
diff --git a/libide/template/ide-template-base.c b/libide/template/ide-template-base.c
new file mode 100644
index 0000000..5cfd589
--- /dev/null
+++ b/libide/template/ide-template-base.c
@@ -0,0 +1,554 @@
+/* ide-template-base.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "ide-template-base.h"
+
+#define TIMEOUT_INTERVAL_MSEC 17
+#define TIMEOUT_DURATION_MSEC 2
+
+typedef struct
+{
+ TmplTemplateLocator *locator;
+ GArray *files;
+
+ guint has_expanded : 1;
+} IdeTemplateBasePrivate;
+
+typedef struct
+{
+ GFile *file;
+ GInputStream *stream;
+ TmplScope *scope;
+ GFile *destination;
+ TmplTemplate *template;
+ gchar *result;
+} FileExpansion;
+
+typedef struct
+{
+ GArray *files;
+ guint index;
+ guint completed;
+} ExpansionTask;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (IdeTemplateBase, ide_template_base, G_TYPE_OBJECT)
+
+enum {
+ PROP_0,
+ PROP_LOCATOR,
+ LAST_PROP
+};
+
+static GParamSpec *properties [LAST_PROP];
+
+/**
+ * ide_template_base_get_locator:
+ * @self: An #IdeTemplateBase
+ *
+ * Fetches the #TmplTemplateLocator used for resolving templates.
+ *
+ * Returns: (transfer none) (nullable): A #TmplTemplateLocator or %NULL.
+ */
+TmplTemplateLocator *
+ide_template_base_get_locator (IdeTemplateBase *self)
+{
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_TEMPLATE_BASE (self), NULL);
+
+ return priv->locator;
+}
+
+void
+ide_template_base_set_locator (IdeTemplateBase *self,
+ TmplTemplateLocator *locator)
+{
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+
+ g_return_if_fail (IDE_IS_TEMPLATE_BASE (self));
+ g_return_if_fail (!locator || TMPL_IS_TEMPLATE_LOCATOR (locator));
+
+ if (priv->has_expanded)
+ {
+ g_warning ("Cannot change template locator after "
+ "ide_template_base_expand_async() has been called.");
+ return;
+ }
+
+ if (g_set_object (&priv->locator, locator))
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LOCATOR]);
+}
+
+static void
+clear_file_expansion (gpointer data)
+{
+ FileExpansion *expansion = data;
+
+ g_clear_object (&expansion->file);
+ g_clear_object (&expansion->stream);
+ g_clear_pointer (&expansion->scope, tmpl_scope_unref);
+ g_clear_object (&expansion->destination);
+ g_clear_object (&expansion->template);
+ g_clear_pointer (&expansion->result, g_free);
+}
+
+static void
+ide_template_base_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeTemplateBase *self = IDE_TEMPLATE_BASE(object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCATOR:
+ g_value_set_object (value, ide_template_base_get_locator (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
+ide_template_base_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeTemplateBase *self = IDE_TEMPLATE_BASE(object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCATOR:
+ ide_template_base_set_locator (self, g_value_get_object (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
+ide_template_base_finalize (GObject *object)
+{
+ IdeTemplateBase *self = (IdeTemplateBase *)object;
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+
+ g_clear_pointer (&priv->files, g_array_unref);
+ g_clear_object (&priv->locator);
+
+ G_OBJECT_CLASS (ide_template_base_parent_class)->finalize (object);
+}
+
+static void
+ide_template_base_class_init (IdeTemplateBaseClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = ide_template_base_finalize;
+ object_class->get_property = ide_template_base_get_property;
+ object_class->set_property = ide_template_base_set_property;
+
+ /**
+ * IdeTemplateBase:locator:
+ *
+ * The #IdeTemplateBase:locator property contains the #TmplTemplateLocator
+ * that should be used to resolve template includes. If %NULL, templates
+ * will not be allowed to include other templates.
+ * directive.
+ */
+ properties [PROP_LOCATOR] =
+ g_param_spec_object ("locator",
+ "Locator",
+ "Locator",
+ TMPL_TYPE_TEMPLATE_LOCATOR,
+ (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, LAST_PROP, properties);
+}
+
+static void
+ide_template_base_init (IdeTemplateBase *self)
+{
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+
+ priv->files = g_array_new (FALSE, TRUE, sizeof (FileExpansion));
+ g_array_set_clear_func (priv->files, clear_file_expansion);
+}
+
+static void
+ide_template_base_parse_worker (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ IdeTemplateBase *self = source_object;
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+ guint i;
+
+ g_assert (G_IS_TASK (task));
+ g_assert (IDE_IS_TEMPLATE_BASE (self));
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ for (i = 0; i < priv->files->len; i++)
+ {
+ FileExpansion *fexp = &g_array_index (priv->files, FileExpansion, i);
+ g_autoptr(TmplTemplate) template = NULL;
+ GError *error = NULL;
+
+ if (fexp->template != NULL)
+ continue;
+
+ template = tmpl_template_new (priv->locator);
+
+ if (!tmpl_template_parse_file (template, fexp->file, cancellable, &error))
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ fexp->template = g_object_ref (template);
+ }
+
+ g_task_return_boolean (task, TRUE);
+}
+
+static void
+ide_template_base_parse_async (IdeTemplateBase *self,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_autoptr(GTask) task = NULL;
+
+ g_assert (IDE_IS_TEMPLATE_BASE (self));
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ task = g_task_new (self, cancellable, callback, user_data);
+ g_task_run_in_thread (task, ide_template_base_parse_worker);
+}
+
+static gboolean
+ide_template_base_parse_finish (IdeTemplateBase *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_assert (IDE_IS_TEMPLATE_BASE (self));
+ g_assert (G_IS_TASK (result));
+
+ return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void
+ide_template_base_replace_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GFile *file = (GFile *)object;
+ g_autoptr(GTask) task = user_data;
+ GError *error = NULL;
+ ExpansionTask *expansion;
+
+ g_assert (G_IS_FILE (file));
+ g_assert (G_IS_ASYNC_RESULT (result));
+ g_assert (G_IS_TASK (task));
+
+ expansion = g_task_get_task_data (task);
+
+ g_assert (expansion != NULL);
+ g_assert (expansion->files != NULL);
+
+ expansion->completed++;
+
+ if (!g_file_replace_contents_finish (file, result, NULL, &error))
+ {
+ if (!g_task_get_completed (task))
+ g_task_return_error (task, error);
+ else
+ g_error_free (error);
+ return;
+ }
+
+ if (expansion->completed == expansion->files->len)
+ {
+ if (!g_task_get_completed (task))
+ g_task_return_boolean (task, TRUE);
+ }
+}
+
+static gboolean
+ide_template_base_expand (GTask *task)
+{
+ ExpansionTask *expansion;
+ gint64 end;
+ gint64 now;
+
+ g_assert (G_IS_TASK (task));
+
+ expansion = g_task_get_task_data (task);
+
+ g_assert (expansion != NULL);
+ g_assert (expansion->files != NULL);
+
+ /*
+ * We will only run for up to 2 milliseconds before we want to yield
+ * back to the main loop and schedule future expansions as low-priority
+ * so that we do not block the frame-clock;
+ */
+ for (end = (now = g_get_monotonic_time ()) + ((G_USEC_PER_SEC / 1000) * TIMEOUT_DURATION_MSEC);
+ now < end;
+ now = g_get_monotonic_time ())
+ {
+ FileExpansion *fexp;
+ GError *error = NULL;
+
+ g_assert (expansion->index <= expansion->files->len);
+
+ if (expansion->index == expansion->files->len)
+ break;
+
+ fexp = &g_array_index (expansion->files, FileExpansion, expansion->index);
+
+ g_assert (fexp != NULL);
+ g_assert (fexp->template != NULL);
+ g_assert (fexp->scope != NULL);
+ g_assert (fexp->result == NULL);
+
+ fexp->result = tmpl_template_expand_string (fexp->template, fexp->scope, &error);
+
+ if (fexp->result == NULL)
+ {
+ g_task_return_error (task, error);
+ return G_SOURCE_REMOVE;
+ }
+
+ expansion->index++;
+ }
+
+ /*
+ * If we have completed expanding all the templates, we need to start
+ * writing the results to the destination files asynchronously, and in
+ * parallel. When all of the async operations have completed, we will
+ * cleanup and complete the task.
+ */
+ if (expansion->index == expansion->files->len)
+ {
+ guint i;
+
+ expansion->completed = 0;
+
+ //ide_template_base_make_directories (task);
+
+ for (i = 0; i < expansion->files->len; i++)
+ {
+ g_autoptr(GFile) directory = NULL;
+ FileExpansion *fexp;
+
+ fexp = &g_array_index (expansion->files, FileExpansion, i);
+
+ g_assert (fexp != NULL);
+ g_assert (G_IS_FILE (fexp->destination));
+ g_assert (fexp->result != NULL);
+
+ directory = g_file_get_parent (fexp->destination);
+
+ g_file_replace_contents_async (fexp->destination,
+ fexp->result,
+ strlen (fexp->result),
+ NULL,
+ FALSE,
+ G_FILE_CREATE_REPLACE_DESTINATION,
+ g_task_get_cancellable (task),
+ ide_template_base_replace_cb,
+ g_object_ref (task));
+ }
+
+ return G_SOURCE_REMOVE;
+ }
+
+ return G_SOURCE_CONTINUE;
+}
+
+static void
+ide_template_base_expand_parse_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ IdeTemplateBase *self = (IdeTemplateBase *)object;
+ g_autoptr(GTask) task = user_data;
+ GError *error = NULL;
+
+ g_assert (IDE_IS_TEMPLATE_BASE (self));
+
+ if (!ide_template_base_parse_finish (self, result, &error))
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ g_timeout_add_full (G_PRIORITY_LOW,
+ TIMEOUT_INTERVAL_MSEC,
+ (GSourceFunc)ide_template_base_expand,
+ g_object_ref (task),
+ g_object_unref);
+}
+
+void
+ide_template_base_expand_all_async (IdeTemplateBase *self,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+ g_autoptr(GTask) task = NULL;
+ ExpansionTask *task_data;
+
+ g_return_if_fail (IDE_IS_TEMPLATE_BASE (self));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ task_data = g_new0 (ExpansionTask, 1);
+ task_data->files = priv->files;
+ task_data->index = 0;
+ task_data->completed = 0;
+
+ /*
+ * The expand process will need to call tmpl_template_expand() and we want
+ * that to happen in the main loop so that all scoped objects need not be
+ * thread-safe.
+ *
+ * Therefore, the first step is to asynchronously load all of the templates
+ * from storage. After that, we will expand the templates into memory,
+ * being careful about how long we run per-cycle in the main-loop. If we
+ * run too long, we risk adding jitter to the frame-clock and causing UI
+ * elements to feel sluggish.
+ *
+ * Once we have all of our templates expanded, we progress to asynchronously
+ * write them to the requested underlying storage.
+ */
+ task = g_task_new (self, cancellable, callback, user_data);
+ g_task_set_task_data (task, task_data, g_free);
+
+ /*
+ * You can only call ide_template_base_expand_async() once, since we maintain
+ * a bunch of state inline.
+ */
+ if (priv->has_expanded)
+ {
+ g_task_return_new_error (task,
+ G_IO_ERROR,
+ G_IO_ERROR_PENDING,
+ "%s() has already been called.",
+ G_STRFUNC);
+ return;
+ }
+
+ priv->has_expanded = TRUE;
+
+ /*
+ * If we have nothing to do, we still need to preserve our "executed" state.
+ * So if there is nothing to do, short circuit now.
+ */
+ if (priv->files->len == 0)
+ {
+ g_task_return_boolean (task, TRUE);
+ return;
+ }
+
+ ide_template_base_parse_async (self,
+ cancellable,
+ ide_template_base_expand_parse_cb,
+ g_object_ref (task));
+}
+
+gboolean
+ide_template_base_expand_all_finish (IdeTemplateBase *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (IDE_IS_TEMPLATE_BASE (self), FALSE);
+ g_return_val_if_fail (G_IS_TASK (result), FALSE);
+
+ return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+void
+ide_template_base_add_resource (IdeTemplateBase *self,
+ const gchar *resource_path,
+ GFile *destination,
+ TmplScope *scope)
+{
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+ FileExpansion expansion = { 0 };
+ g_autofree gchar *uri = NULL;
+
+ g_return_if_fail (IDE_IS_TEMPLATE_BASE (self));
+ g_return_if_fail (resource_path != NULL);
+ g_return_if_fail (G_IS_FILE (destination));
+
+ if (priv->has_expanded)
+ {
+ g_warning ("%s() called after ide_template_base_expand_async(). "
+ "Ignoring request to add resource.",
+ G_STRFUNC);
+ return;
+ }
+
+ uri = g_strdup_printf ("resource://%s", resource_path);
+
+ expansion.file = g_file_new_for_uri (uri);
+ expansion.stream = NULL;
+ expansion.scope = scope ? tmpl_scope_ref (scope) : tmpl_scope_new ();
+ expansion.destination = g_object_ref (destination);
+ expansion.result = NULL;
+
+ g_array_append_val (priv->files, expansion);
+}
+
+void
+ide_template_base_add_path (IdeTemplateBase *self,
+ const gchar *path,
+ GFile *destination,
+ TmplScope *scope)
+{
+ IdeTemplateBasePrivate *priv = ide_template_base_get_instance_private (self);
+ FileExpansion expansion = { 0 };
+
+ g_return_if_fail (IDE_IS_TEMPLATE_BASE (self));
+ g_return_if_fail (path != NULL);
+ g_return_if_fail (G_IS_FILE (destination));
+
+ if (priv->has_expanded)
+ {
+ g_warning ("%s() called after ide_template_base_expand_async(). "
+ "Ignoring request to add resource.",
+ G_STRFUNC);
+ return;
+ }
+
+ expansion.file = g_file_new_for_path (path);
+ expansion.stream = NULL;
+ expansion.scope = scope ? tmpl_scope_ref (scope) : tmpl_scope_new ();
+ expansion.destination = g_object_ref (destination);
+ expansion.result = NULL;
+
+ g_array_append_val (priv->files, expansion);
+}
diff --git a/libide/template/ide-template-base.h b/libide/template/ide-template-base.h
new file mode 100644
index 0000000..eda2dfb
--- /dev/null
+++ b/libide/template/ide-template-base.h
@@ -0,0 +1,56 @@
+/* ide-template-base.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_TEMPLATE_BASE_H
+#define IDE_TEMPLATE_BASE_H
+
+#include <tmpl-glib.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TEMPLATE_BASE (ide_template_base_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (IdeTemplateBase, ide_template_base, IDE, TEMPLATE_BASE, GObject)
+
+struct _IdeTemplateBaseClass
+{
+ GObjectClass parent_class;
+};
+
+TmplTemplateLocator *ide_template_base_get_locator (IdeTemplateBase *self);
+void ide_template_base_set_locator (IdeTemplateBase *self,
+ TmplTemplateLocator *locator);
+void ide_template_base_add_resource (IdeTemplateBase *self,
+ const gchar *resource_path,
+ GFile *destination,
+ TmplScope *scope);
+void ide_template_base_add_path (IdeTemplateBase *self,
+ const gchar *path,
+ GFile *destination,
+ TmplScope *scope);
+void ide_template_base_expand_all_async (IdeTemplateBase *self,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean ide_template_base_expand_all_finish (IdeTemplateBase *self,
+ GAsyncResult *result,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* IDE_TEMPLATE_BASE_H */
diff --git a/libide/template/ide-template-provider.c b/libide/template/ide-template-provider.c
new file mode 100644
index 0000000..45357f7
--- /dev/null
+++ b/libide/template/ide-template-provider.c
@@ -0,0 +1,53 @@
+/* ide-template-provider.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-template-provider.h"
+
+G_DEFINE_INTERFACE (IdeTemplateProvider, ide_template_provider, G_TYPE_OBJECT)
+
+static GList *
+ide_template_provider_real_get_project_templates (IdeTemplateProvider *self)
+{
+ return NULL;
+}
+
+static void
+ide_template_provider_default_init (IdeTemplateProviderInterface *iface)
+{
+ iface->get_project_templates = ide_template_provider_real_get_project_templates;
+}
+
+/**
+ * ide_template_provider_get_project_templates:
+ * @self: An #IdeTemplateProvider
+ *
+ * Gets a list of templates for this provider.
+ *
+ * Plugins should implement this interface to feed #IdeProjectTemplate's into
+ * the project creation workflow.
+ *
+ * Returns: (transfer full) (element-type Ide.ProjectTemplate): A #GList of
+ * #IdeProjectTemplate instances.
+ */
+GList *
+ide_template_provider_get_project_templates (IdeTemplateProvider *self)
+{
+ g_return_val_if_fail (IDE_IS_TEMPLATE_PROVIDER (self), NULL);
+
+ return IDE_TEMPLATE_PROVIDER_GET_IFACE (self)->get_project_templates (self);
+}
diff --git a/libide/template/ide-template-provider.h b/libide/template/ide-template-provider.h
new file mode 100644
index 0000000..a367be7
--- /dev/null
+++ b/libide/template/ide-template-provider.h
@@ -0,0 +1,41 @@
+/* ide-template-provider.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_TEMPLATE_PROVIDER_H
+#define IDE_TEMPLATE_PROVIDER_H
+
+#include "ide-project-template.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TEMPLATE_PROVIDER (ide_template_provider_get_type())
+
+G_DECLARE_INTERFACE (IdeTemplateProvider, ide_template_provider, IDE, TEMPLATE_PROVIDER, GObject)
+
+struct _IdeTemplateProviderInterface
+{
+ GTypeInterface parent_iface;
+
+ GList *(*get_project_templates) (IdeTemplateProvider *self);
+};
+
+GList *ide_template_provider_get_project_templates (IdeTemplateProvider *self);
+
+G_END_DECLS
+
+#endif /* IDE_TEMPLATE_PROVIDER_H */
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 7e9628a..b316c78 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -5,6 +5,7 @@ SUBDIRS = \
command-bar \
contributing \
c-pack \
+ create-project \
ctags \
devhelp \
file-search \
@@ -15,6 +16,7 @@ SUBDIRS = \
html-completion \
html-preview \
jedi \
+ library-template \
project-tree \
python-gi-imports-completion \
mingw \
diff --git a/plugins/create-project/Makefile.am b/plugins/create-project/Makefile.am
new file mode 100644
index 0000000..f840f31
--- /dev/null
+++ b/plugins/create-project/Makefile.am
@@ -0,0 +1,46 @@
+if ENABLE_CREATE_PROJECT_PLUGIN
+
+DISTCLEANFILES =
+BUILT_SOURCES =
+CLEANFILES =
+EXTRA_DIST = $(plugin_DATA)
+
+plugindir = $(libdir)/gnome-builder/plugins
+plugin_LTLIBRARIES = libcreate-project-plugin.la
+dist_plugin_DATA = create-project.plugin
+
+libcreate_project_plugin_la_SOURCES = \
+ gbp-create-project-plugin.c \
+ gbp-create-project-tool.c \
+ gbp-create-project-tool.h \
+ $(NULL)
+
+nodist_libcreate_project_plugin_la_SOURCES = \
+ gbp-create-project-resources.c \
+ gbp-create-project-resources.h
+
+libcreate_project_plugin_la_CFLAGS = \
+ $(LIBIDE_CFLAGS) \
+ $(OPTIMIZE_CFLAGS) \
+ -I$(top_srcdir)/libide \
+ -I$(top_srcdir)/contrib/egg \
+ $(NULL)
+
+libcreate_project_plugin_la_LDFLAGS = \
+ $(OPTIMIZE_LDFLAGS) \
+ -avoid-version \
+ -module \
+ -export-regex peas_register_types \
+ $(NULL)
+
+glib_resources_c = gbp-create-project-resources.c
+glib_resources_h = gbp-create-project-resources.h
+glib_resources_xml = gbp-create-project.gresource.xml
+glib_resources_namespace = gbp_create_project
+include $(top_srcdir)/build/autotools/Makefile.am.gresources
+
+include $(top_srcdir)/plugins/Makefile.plugin
+
+endif
+
+-include $(top_srcdir)/git.mk
diff --git a/plugins/create-project/configure.ac b/plugins/create-project/configure.ac
new file mode 100644
index 0000000..6491ff2
--- /dev/null
+++ b/plugins/create-project/configure.ac
@@ -0,0 +1,12 @@
+# --enable-create-project-plugin=yes/no
+AC_ARG_ENABLE([create-project-plugin],
+ [AS_HELP_STRING([--enable-create-project-plugin=@<:@yes/no@:>@],
+ [Build with support for creating projects.])],
+ [enable_create_project_plugin=$enableval],
+ [enable_create_project_plugin=yes])
+
+# for if ENABLE_CREATE_PROJECT_PLUGIN in Makefile.am
+AM_CONDITIONAL(ENABLE_CREATE_PROJECT_PLUGIN, test x$enable_create_project_plugin != xno)
+
+# Ensure our makefile is generated by autoconf
+AC_CONFIG_FILES([plugins/create-project/Makefile])
diff --git a/plugins/create-project/create-project.plugin b/plugins/create-project/create-project.plugin
new file mode 100644
index 0000000..afc340d
--- /dev/null
+++ b/plugins/create-project/create-project.plugin
@@ -0,0 +1,10 @@
+[Plugin]
+Module=create-project-plugin
+Name=Create Project
+Description=Create projects with Builder
+Authors=Christian Hergert <christian hergert me>
+Copyright=Copyright © 2015 Christian Hergert
+Builtin=true
+Hidden=true
+X-Tool-Name=create-project
+X-Tool-Description=Create a new project
diff --git a/plugins/create-project/gbp-create-project-plugin.c
b/plugins/create-project/gbp-create-project-plugin.c
new file mode 100644
index 0000000..70e02bb
--- /dev/null
+++ b/plugins/create-project/gbp-create-project-plugin.c
@@ -0,0 +1,30 @@
+/* gbp-create-project-plugin.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <ide.h>
+#include <libpeas/peas.h>
+
+#include "gbp-create-project-tool.h"
+
+void
+peas_register_types (PeasObjectModule *module)
+{
+ peas_object_module_register_extension_type (module,
+ IDE_TYPE_APPLICATION_TOOL,
+ GBP_TYPE_CREATE_PROJECT_TOOL);
+}
diff --git a/plugins/create-project/gbp-create-project-tool.c
b/plugins/create-project/gbp-create-project-tool.c
new file mode 100644
index 0000000..cecb8e0
--- /dev/null
+++ b/plugins/create-project/gbp-create-project-tool.c
@@ -0,0 +1,352 @@
+/* gbp-create-project-tool.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <glib/gi18n.h>
+#include <libpeas/peas.h>
+
+#include "gbp-create-project-tool.h"
+
+struct _GbpCreateProjectTool
+{
+ GObject parent_instance;
+ gboolean list_templates;
+ gchar **args;
+ gchar *template;
+ GList *project_templates;
+};
+
+static void application_tool_iface_init (IdeApplicationToolInterface *iface);
+
+G_DEFINE_TYPE_EXTENDED (GbpCreateProjectTool, gbp_create_project_tool, G_TYPE_OBJECT, 0,
+ G_IMPLEMENT_INTERFACE (IDE_TYPE_APPLICATION_TOOL,
+ application_tool_iface_init))
+
+static void
+template_providers_foreach_cb (PeasExtensionSet *set,
+ PeasPluginInfo *plugin_info,
+ PeasExtension *exten,
+ gpointer user_data)
+{
+ GbpCreateProjectTool *self = user_data;
+ IdeTemplateProvider *provider = IDE_TEMPLATE_PROVIDER (exten);
+ GList *templates = ide_template_provider_get_project_templates (provider);
+
+ self->project_templates = g_list_concat (self->project_templates, templates);
+}
+
+static void
+gbp_create_project_tool_constructed (GObject *object)
+{
+ GbpCreateProjectTool *self = (GbpCreateProjectTool *)object;
+ PeasEngine *engine = peas_engine_get_default ();
+ PeasExtensionSet *extensions;
+
+ extensions = peas_extension_set_new (engine,
+ IDE_TYPE_TEMPLATE_PROVIDER,
+ NULL);
+ peas_extension_set_foreach (extensions,
+ template_providers_foreach_cb,
+ self);
+ g_clear_object (&extensions);
+
+ G_OBJECT_CLASS (gbp_create_project_tool_parent_class)->constructed (object);
+}
+
+static void
+gbp_create_project_tool_finalize (GObject *object)
+{
+ GbpCreateProjectTool *self = (GbpCreateProjectTool *)object;
+
+ g_list_foreach (self->project_templates, (GFunc)g_object_unref, NULL);
+ g_clear_pointer (&self->project_templates, g_list_free);
+ g_clear_pointer (&self->args, g_strfreev);
+ g_clear_pointer (&self->template, g_free);
+
+ G_OBJECT_CLASS (gbp_create_project_tool_parent_class)->finalize (object);
+}
+
+static void
+gbp_create_project_tool_class_init (GbpCreateProjectToolClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->constructed = gbp_create_project_tool_constructed;
+ object_class->finalize = gbp_create_project_tool_finalize;
+}
+
+static void
+gbp_create_project_tool_init (GbpCreateProjectTool *self)
+{
+}
+
+static void
+gbp_create_project_tool_list_templates (GbpCreateProjectTool *self)
+{
+ const GList *iter;
+
+ g_assert (GBP_IS_CREATE_PROJECT_TOOL (self));
+
+ g_print ("\n");
+
+ for (iter = self->project_templates; iter != NULL; iter = iter->next)
+ {
+ IdeProjectTemplate *template = iter->data;
+ const gchar *id;
+
+ if (NULL != (id = ide_project_template_get_id (template)))
+ g_print (" %s\n", id);
+ }
+
+ g_print ("\n");
+}
+
+static gboolean
+gbp_create_project_tool_parse (GbpCreateProjectTool *self,
+ GError **error)
+{
+ g_autoptr(GOptionContext) context = NULL;
+ GOptionEntry entries[] = {
+ { "list-templates", 'l', 0, G_OPTION_ARG_NONE, &self->list_templates,
+ N_("List available templates") },
+ { "template", 't', 0, G_OPTION_ARG_STRING, &self->template,
+ N_("Project template to generate") },
+ { NULL }
+ };
+
+ g_assert (GBP_IS_CREATE_PROJECT_TOOL (self));
+
+ context = g_option_context_new (_("create-project [OPTION...] PROJECT_NAME"));
+ g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+
+ if (!g_option_context_parse_strv (context, &self->args, error))
+ return FALSE;
+
+ return TRUE;
+}
+
+static IdeProjectTemplate *
+find_template (GbpCreateProjectTool *self)
+{
+ const GList *iter;
+
+ g_assert (GBP_IS_CREATE_PROJECT_TOOL (self));
+ g_assert (self->template != NULL);
+
+ for (iter = self->project_templates; iter != NULL; iter = iter->next)
+ {
+ IdeProjectTemplate *template = IDE_PROJECT_TEMPLATE (iter->data);
+ const gchar *id = ide_project_template_get_id (template);
+
+ if (g_strcmp0 (self->template, id) == 0)
+ return template;
+ }
+
+ return NULL;
+}
+
+static gboolean
+validate_name (GbpCreateProjectTool *self,
+ const gchar *name,
+ GError **error)
+{
+ for (; *name; name = g_utf8_next_char (name))
+ {
+ gunichar ch = g_utf8_get_char (name);
+
+ switch (ch)
+ {
+ default:
+ if (isascii (ch))
+ continue;
+ /* Fall through */
+ case '=':
+ case ':':
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_DATA,
+ _("Filename must be ascii and may not contain : or ="));
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+static void
+extract_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ IdeProjectTemplate *template = (IdeProjectTemplate *)object;
+ g_autoptr(GTask) task = user_data;
+ GError *error = NULL;
+
+ g_assert (IDE_IS_PROJECT_TEMPLATE (template));
+ g_assert (G_IS_ASYNC_RESULT (result));
+ g_assert (G_IS_TASK (task));
+
+ if (!ide_project_template_expand_finish (template, result, &error))
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ g_task_return_int (task, 0);
+}
+
+static gboolean
+extract_params (GbpCreateProjectTool *self,
+ GHashTable *params,
+ GError **error)
+{
+ gint i;
+
+ g_assert (GBP_IS_CREATE_PROJECT_TOOL (self));
+ g_assert (params != NULL);
+
+ if (self->args && g_strv_length (self->args) > 2)
+ {
+ for (i = 2; self->args [i]; i++)
+ {
+ const gchar *arg = self->args [i];
+ const gchar *eq;
+
+ if ((eq = strchr (arg, '=')) != NULL)
+ {
+ g_autofree gchar *value = NULL;
+ gchar *key;
+ GVariant *var;
+
+ key = g_strndup (arg, (eq - arg));
+ value = g_strdup (eq + 1);
+
+ var = g_variant_parse (NULL, value, NULL, NULL, NULL);
+ if (var == NULL)
+ var = g_variant_new_string (value);
+
+ g_hash_table_insert (params, key, g_variant_ref_sink (var));
+ }
+ }
+ }
+
+ return TRUE;
+}
+
+static void
+gbp_create_project_tool_run_async (IdeApplicationTool *tool,
+ const gchar * const *arguments,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GbpCreateProjectTool *self = (GbpCreateProjectTool *)tool;
+ IdeProjectTemplate *template;
+ g_autoptr(GTask) task = NULL;
+ g_autoptr(GHashTable) params = NULL;
+ const gchar *name;
+ GError *error = NULL;
+
+ g_assert (GBP_IS_CREATE_PROJECT_TOOL (self));
+
+ task = g_task_new (self, cancellable, callback, user_data);
+
+ /* pretend that "create-project" is argv[0] */
+ self->args = g_strdupv ((gchar **)&arguments[1]);
+
+ if (!gbp_create_project_tool_parse (self, &error))
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ if (self->list_templates)
+ {
+ gbp_create_project_tool_list_templates (self);
+ g_task_return_int (task, 0);
+ return;
+ }
+
+ if (!self->args || g_strv_length (self->args) < 2)
+ {
+ g_printerr (_("Please specify a project name.\n"));
+ g_task_return_int (task, 1);
+ return;
+ }
+
+ name = self->args [1];
+
+ if (!validate_name (self, name, &error))
+ {
+ g_printerr ("%s\n", error->message);
+ g_task_return_error (task, error);
+ return;
+ }
+
+ if (!self->template || !(template = find_template (self)))
+ {
+ g_printerr (_("Please specify a project template with --template=\n"));
+ gbp_create_project_tool_list_templates (self);
+ g_task_return_int (task, 1);
+ return;
+ }
+
+ params = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ g_free,
+ (GDestroyNotify)g_variant_unref);
+
+ if (!extract_params (self, params, &error))
+ {
+ g_printerr ("%s\n", error->message);
+ g_task_return_error (task, error);
+ return;
+ }
+
+ g_hash_table_insert (params,
+ g_strdup ("name"),
+ g_variant_ref_sink (g_variant_new_string (name)));
+
+ ide_project_template_expand_async (template,
+ params,
+ NULL,
+ extract_cb,
+ g_object_ref (task));
+}
+
+static gint
+gbp_create_project_tool_run_finish (IdeApplicationTool *tool,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_assert (GBP_IS_CREATE_PROJECT_TOOL (tool));
+ g_assert (G_IS_TASK (result));
+
+ return g_task_propagate_int (G_TASK (result), error);
+}
+
+static void
+application_tool_iface_init (IdeApplicationToolInterface *iface)
+{
+ iface->run_async = gbp_create_project_tool_run_async;
+ iface->run_finish = gbp_create_project_tool_run_finish;
+}
diff --git a/plugins/create-project/gbp-create-project-tool.h
b/plugins/create-project/gbp-create-project-tool.h
new file mode 100644
index 0000000..1fa888e
--- /dev/null
+++ b/plugins/create-project/gbp-create-project-tool.h
@@ -0,0 +1,32 @@
+/* gbp-create-project-tool.h
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GBP_CREATE_PROJECT_TOOL_H
+#define GBP_CREATE_PROJECT_TOOL_H
+
+#include <ide.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_CREATE_PROJECT_TOOL (gbp_create_project_tool_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpCreateProjectTool, gbp_create_project_tool, GBP, CREATE_PROJECT_TOOL, GObject)
+
+G_END_DECLS
+
+#endif /* GBP_CREATE_PROJECT_TOOL_H */
diff --git a/plugins/create-project/gbp-create-project.gresource.xml
b/plugins/create-project/gbp-create-project.gresource.xml
new file mode 100644
index 0000000..7c5c913
--- /dev/null
+++ b/plugins/create-project/gbp-create-project.gresource.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+ <gresource prefix="/org/gnome/builder/plugins/create-project-plugin">
+ </gresource>
+</gresources>
diff --git a/plugins/library-template/Makefile.am b/plugins/library-template/Makefile.am
new file mode 100644
index 0000000..6faba6e
--- /dev/null
+++ b/plugins/library-template/Makefile.am
@@ -0,0 +1,18 @@
+if ENABLE_LIBRARY_TEMPLATE_PLUGIN
+
+EXTRA_DIST = $(plugin_DATA)
+
+plugindir = $(libdir)/gnome-builder/plugins
+dist_plugin_DATA = library-template.plugin
+
+moduledir = $(libdir)/gnome-builder/plugins/library_template
+dist_module_DATA = library_template/__init__.py
+
+resourcedir = $(datadir)/gnome-builder/plugins/library_template
+dist_resource_DATA =
+
+endif
+
+GITIGNOREFILES = library_template/__pycache__
+
+-include $(top_srcdir)/git.mk
diff --git a/plugins/library-template/configure.ac b/plugins/library-template/configure.ac
new file mode 100644
index 0000000..82ef9a4
--- /dev/null
+++ b/plugins/library-template/configure.ac
@@ -0,0 +1,11 @@
+AC_ARG_ENABLE([library-template-plugin],
+ [AS_HELP_STRING([--enable-library-template-plugin=@<:@auto/yes/no@:>@],
+ [Build with support for creating library projects.])],
+ [enable_library_template_plugin=$enableval],
+ [enable_library_template_plugin=yes])
+
+# for if ENABLE_LIBRARY_TEMPLATE_PLUGIN in Makefile.am
+AM_CONDITIONAL(ENABLE_LIBRARY_TEMPLATE_PLUGIN, test x$enable_library_template_plugin = xyes)
+
+# Ensure our makefile is generated by autoconf
+AC_CONFIG_FILES([plugins/library-template/Makefile])
diff --git a/plugins/library-template/library-template.plugin
b/plugins/library-template/library-template.plugin
new file mode 100644
index 0000000..bd9d232
--- /dev/null
+++ b/plugins/library-template/library-template.plugin
@@ -0,0 +1,11 @@
+[Plugin]
+Name=Library Templates
+Description=Provides templates for creating libraries
+Authors=Christian Hergert <christian hergert me>
+Copyright=Copyright © 2016 Christian Hergert
+
+Builtin=true
+Hidden=true
+
+Loader=python3
+Module=library_template
diff --git a/plugins/library-template/library_template/__init__.py
b/plugins/library-template/library_template/__init__.py
new file mode 100644
index 0000000..31746da
--- /dev/null
+++ b/plugins/library-template/library_template/__init__.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+
+#
+# __init__.py
+#
+# Copyright (C) 2016 Christian Hergert <chergert redhat com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+from gettext import gettext as _
+
+import gi
+
+gi.require_version('Template', '1.0')
+
+from gi.repository import Ide
+from gi.repository import Gio
+from gi.repository import GLib
+from gi.repository import GObject
+from gi.repository import Peas
+from gi.repository import Template
+
+def get_module_data_path(name):
+ engine = Peas.Engine.get_default()
+ plugin = engine.get_plugin_info('library_template')
+ data_dir = plugin.get_data_dir()
+ return GLib.build_filenamev([data_dir, name])
+
+class LibraryTemplateProvider(GObject.Object, Ide.TemplateProvider):
+ def do_get_project_templates(self):
+ return [LibraryProjectTemplate()]
+
+class LibraryProjectTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
+ def do_get_id(self):
+ return 'shared-library'
+
+ def do_get_name(self):
+ return _("Shared Library")
+
+ def do_get_description(self):
+ return _("Create a new autotools project with a shared library")
+
+ def do_get_languages(self):
+ return ['C', 'Python']
+
+ def do_get_icon_name(self):
+ return 'template-shared-library'
+
+ def do_expand_async(self, params, cancellable, callback, data):
+ name = params['name'].get_string()
+ scope = Template.Scope.new()
+ directory = Gio.File.new_for_path(name)
+
+ files = { 'configure.ac': 'configure.ac' }
+
+ for src,dst in files.items():
+ path = get_module_data_path(src)
+ print(path)
+ destination = directory.get_child(dst)
+ self.add_path(path, destination, scope)
+
+ task = Gio.Task.new(self, cancellable, callback)
+ self.expand_all_async(cancellable, self.expand_all_cb, task)
+
+ def do_expand_finish(self, result):
+ return result.propagate_boolean()
+
+ def expand_all_cb(self, obj, result, task):
+ try:
+ self.expand_all_finish(result)
+ task.return_boolean(True)
+ except Exception as exc:
+ print(exc)
+ task.return_error(GLib.Error(exc))
diff --git a/plugins/library-template/library_template/configure.ac
b/plugins/library-template/library_template/configure.ac
new file mode 100644
index 0000000..d10411c
--- /dev/null
+++ b/plugins/library-template/library_template/configure.ac
@@ -0,0 +1,2 @@
+AC_INIT([2.69])
+
diff --git a/plugins/vala-pack/Makefile.am b/plugins/vala-pack/Makefile.am
index 2d26850..e47a235 100644
--- a/plugins/vala-pack/Makefile.am
+++ b/plugins/vala-pack/Makefile.am
@@ -26,11 +26,13 @@ libvala_pack_plugin_la_VALAFLAGS = \
--target-glib=2.44 \
--thread \
--vapidir $(top_builddir)/libide \
+ --vapidir $(top_builddir)/contrib/tmpl \
--pkg gtksourceview-3.0 \
--pkg libide-1.0 \
--pkg libpeas-1.0 \
--pkg libvala-$(VALA_VERSION) \
--pkg posix \
+ --pkg template-glib-1.0 \
$(NULL)
libvala_pack_plugin_la_CFLAGS = \
@@ -39,6 +41,7 @@ libvala_pack_plugin_la_CFLAGS = \
$(VALA_CFLAGS) \
-I$(top_srcdir)/libide \
-I$(top_srcdir)/contrib/egg \
+ -I$(top_srcdir)/contrib/tmpl \
$(NULL)
libvala_pack_plugin_la_LDFLAGS = \
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]