Commit e4904cce authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

vala/valacodecontext.vala vala/valasourcefile.vala filename, line, and

2006-05-06  Jürg Billeter  <j@bitron.ch>

	* vala/valacodecontext.vala
	* vala/valasourcefile.vala
	* vala/valasourcereference.vala: filename, line, and comment container
	* vala/valacodenode.vala: abstract code node
	* vala/valanamespace.vala: namespace node
	* vala/valaclass.vala: class node
	* vala/valacodevisitor.vala: abstract syntax tree visitor
	* vala/valaparser.vala: vala parser
	* vala/valacodegenerator.vala: code generator skeleton
	* compiler/valacompiler.vala: compiler driver
	* bindings/GLib.vala: add bindings for GList and GOptionContext
	* valac/scanner.l: support multi-line comments, ABSTRACT, INTERFACE,
	  READONLY
	* valac/parser.y: replace AT by HASH, support readonly fields
	* valac/context.h: small fixes
	* valac/context.c: small fixes
	* valac/generator.c: fix override methods, basic support for properties

svn path=/trunk/; revision=13
parent c300d9c3
2006-05-06 Jürg Billeter <j@bitron.ch>
* vala/valacodecontext.vala
* vala/valasourcefile.vala
* vala/valasourcereference.vala: filename, line, and comment container
* vala/valacodenode.vala: abstract code node
* vala/valanamespace.vala: namespace node
* vala/valaclass.vala: class node
* vala/valacodevisitor.vala: abstract syntax tree visitor
* vala/valaparser.vala: vala parser
* vala/valacodegenerator.vala: code generator skeleton
* compiler/valacompiler.vala: compiler driver
* bindings/GLib.vala: add bindings for GList and GOptionContext
* valac/scanner.l: support multi-line comments, ABSTRACT, INTERFACE,
READONLY
* valac/parser.y: replace AT by HASH, support readonly fields
* valac/context.h: small fixes
* valac/context.c: small fixes
* valac/generator.c: fix override methods, basic support for properties
2006-05-03 Jürg Billeter <j@bitron.ch>
* valac/scanner.l: support OP_NEG, CONSTRUCT, FOREACH, IN
......
/* GLib.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using Vala;
[CCode (cname = "gboolean")]
public struct bool {
}
[CCode (cname = "gpointer")]
public struct pointer {
}
public struct char {
}
[CCode (cname = "unsigned char")]
public struct uchar {
}
public struct int {
}
[CCode (cname = "unsigned int")]
public struct uint {
}
public struct short {
}
[CCode (cname = "unsigned short")]
public struct ushort {
}
public struct long {
}
[CCode (cname = "unsigned long")]
public struct ulong {
}
[CCode (cname = "gint8")]
public struct int8 {
}
[CCode (cname = "guint8")]
public struct uint8 {
}
[CCode (cname = "gint16")]
public struct int16 {
}
[CCode (cname = "guint16")]
public struct uint16 {
}
[CCode (cname = "gint32")]
public struct int32 {
}
[CCode (cname = "guint32")]
public struct uint32 {
}
[CCode (cname = "gint64")]
public struct int64 {
}
[CCode (cname = "guint64")]
public struct uint64 {
}
[ReferenceType ()]
[CCode (cname = "char")]
public struct string {
[CCode (cname = "g_str_has_suffix")]
public bool has_suffix (string suffix);
[CCode (cname = "g_strdup_printf")]
public string# printf (string args);
[CCode (cname = "g_strconcat")]
public string# concat (string string2);
}
[Import ()]
[CCode (cname = "g", cprefix = "G", include_filename = "glib/glib.h")]
namespace GLib {
[ReferenceType ()]
public struct Error {
}
[ReferenceType ()]
[CCode (cname = "FILE")]
public struct File {
[CCode (cname = "fprintf")]
public void printf (string format);
}
public static GLib.File stderr;
[Unknown (reference_type = true)]
[ReferenceType ()]
public struct OptionContext {
public static OptionContext# @new (string parameter_string);
public bool parse (ref int argc, out string[] argv, out Error error);
public void set_help_enabled (bool help_enabled);
public void add_main_entries (OptionEntry[] entries, string translation_domain);
}
public enum OptionArg {
NONE,
STRING,
INT,
CALLBACK,
FILENAME,
STRING_ARRAY,
FILENAME_ARRAY
}
public flags OptionFlags {
HIDDEN,
IN_MAIN,
REVERSE,
NO_ARG,
FILENMAE,
OPTIONAL_ARG,
NOALIAS
}
public struct OptionEntry {
string long_name;
char short_name;
int flags_;
OptionArg arg;
pointer arg_data;
string description;
string arg_description;
}
[ReferenceType ()]
public struct List<G> {
[ReturnsModifiedPointer ()]
public void append (G data);
[ReturnsModifiedPointer ()]
public void prepend (G data);
[ReturnsModifiedPointer ()]
public void insert (G data, int position);
[ReturnsModifiedPointer ()]
public void insert_before (List<G> sibling, G data);
[ReturnsModifiedPointer ()]
public void remove (G data);
[ReturnsModifiedPointer ()]
public void remove_link (List<G> llink);
[ReturnsModifiedPointer ()]
public void remove_all (G data);
public void free ();
public uint length ();
public List<G># copy ();
[ReturnsModifiedPointer ()]
public void reverse ();
[ReturnsModifiedPointer ()]
public void concat (List<G># list2);
public List<G> first ();
public List<G> last ();
public List<G> nth (uint n);
public pointer nth_data (uint n);
public List<G> nth_prev (uint n);
public List<G> find (G data);
public int position (List<G> llink);
public int index (G data);
public G data;
public List<G> next;
public List<G> prev;
}
}
/* valacompiler.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
class Compiler {
static string directory;
static int version;
static string[] sources;
CodeContext context;
const OptionEntry[] options = {
{ "directory", 'd', 0, OptionArg.FILENAME, out directory, "Output directory", "DIRECTORY" },
{ "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
{ "", 0, 0, OptionArg.FILENAME_ARRAY, out sources, null, "FILE..." },
{ null }
};
void run () {
context = new CodeContext ();
foreach (string source in sources) {
context.add_source_file (new SourceFile (filename = source));
}
var parser = new Parser ();
parser.parse (context);
var code_generator = new CodeGenerator ();
code_generator.emit (context);
}
static int main (int argc, string[] argv) {
Error err = null;
var opt_context = OptionContext.@new ("- Vala Compiler");
opt_context.set_help_enabled (true);
opt_context.add_main_entries (options, null);
opt_context.parse (ref argc, out argv, out err);
if (err != null) {
return 1;
}
if (sources == null) {
stderr.printf ("No source file specified.\n");
return 1;
}
foreach (string source in sources) {
if (!source.has_suffix (".vala")) {
stderr.printf ("Only .vala source files supported.\n");
return 1;
}
}
var compiler = new Compiler ();
compiler.run ();
return 0;
}
}
}
/* valaclass.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class Class : CodeNode {
public readonly string# name;
public readonly SourceReference# source_reference;
public static Class# @new (string name, SourceReference source) {
return (new Class (name = name, source_reference = source));
}
public override void accept (CodeVisitor visitor) {
visitor.visit_class (this);
}
}
}
/* valacodecontext.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class CodeContext {
List<SourceFile#># source_files;
public void add_source_file (SourceFile file) {
source_files.append (file);
}
public void accept (CodeVisitor visitor) {
foreach (SourceFile file in source_files) {
file.accept (visitor);
}
}
}
}
/* valacodegenerator.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class CodeGenerator : CodeVisitor {
public void emit (CodeContext context) {
context.accept (this);
}
public override void visit_source_file (SourceFile source_file) {
}
public override void visit_namespace (Namespace ns) {
}
public override void visit_class (Class cl) {
}
}
}
/* valacodenode.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
namespace Vala {
public abstract class CodeNode {
public abstract void accept (CodeVisitor visitor);
}
}
/* valacodevisitor.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public abstract class CodeVisitor {
public virtual void visit_source_file (SourceFile source_file) {
}
public virtual void visit_namespace (Namespace ns) {
}
public virtual void visit_class (Class cl) {
}
}
}
/* valanamespace.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class Namespace : CodeNode {
public readonly string# name;
public readonly SourceReference# source_reference;
List<Class#># classes;
public static Namespace# @new (string name, SourceReference source) {
return (new Namespace (name = name, source_reference = source));
}
public void add_class (Class cl) {
classes.append (cl);
}
public void remove_class (Class cl) {
classes.remove (cl);
}
public List<Class># get_classes () {
return classes.copy ();
}
public override void accept (CodeVisitor visitor) {
visitor.visit_namespace (this);
foreach (Class cl in classes) {
cl.accept (visitor);
}
}
}
}
/* valaparser.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class Parser : CodeVisitor {
string# comment;
public void parse (CodeContext context) {
context.accept (this);
}
public override void visit_source_file (SourceFile source_file) {
parse_file (source_file);
}
public void push_comment (string comment_item) {
if (comment == null) {
comment = comment_item;
} else {
comment = "%s\n%s".printf (comment, comment_item);
}
}
public string# pop_comment () {
string# result = comment;
comment = null;
return result;
}
[Import ()]
public void parse_file (SourceFile source_file);
}
}
/* valasourcefile.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class SourceFile {
public readonly string# filename;
Namespace# global_namespace = new Namespace ();
List<Namespace#># namespaces;
public void add_namespace (Namespace ns) {
namespaces.append (ns);
}
public Namespace get_global_namespace () {
return global_namespace;
}
public void accept (CodeVisitor visitor) {
visitor.visit_source_file (this);
global_namespace.accept (visitor);
foreach (Namespace ns in namespaces) {
ns.accept (visitor);
}
}
}
}
/* valasourcereference.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library 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 of the License, or (at your option) any later version.
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
namespace Vala {
public class SourceReference {
public readonly SourceFile file;
public readonly int first_line;
public readonly int first_column;
public readonly int last_line;
public readonly int last_column;
public readonly string# comment;
public static SourceReference# @new (SourceFile file, int first_line, int first_column, int last_line, int last_column, string comment) {
return (new SourceReference (file = file, first_line = first_line, first_column = first_column, last_line = last_line, last_column = last_column, comment = comment));
}
public string to_string () {
return ("%s:%d.%d-%d.%d".printf (file.filename, first_line, first_column, last_line, last_column));
}
}
}
......@@ -295,34 +295,6 @@ vala_context_add_fundamental_symbols (ValaContext *context)
symbol = vala_symbol_new (VALA_SYMBOL_TYPE_VOID);
g_hash_table_insert (context->root->symbol_table, "void", symbol);
/* bool */
struct_ = g_new0 (ValaStruct, 1);
struct_->name = "bool";
struct_->namespace = namespace;
struct_->cname = "gboolean";
namespace->structs = g_list_append (namespace->structs, struct_);
/* int */
struct_ = g_new0 (ValaStruct, 1);
struct_->name = g_strdup ("int");
struct_->namespace = namespace;
struct_->cname = g_strdup ("int");
namespace->structs = g_list_append (namespace->structs, struct_);
/* uint */
struct_ = g_new0 (ValaStruct, 1);
struct_->name = "uint";
struct_->namespace = namespace;
struct_->cname = "unsigned int";
namespace->structs = g_list_append (namespace->structs, struct_);
/* pointer */
struct_ = g_new0 (ValaStruct, 1);
struct_->name = "pointer";
struct_->namespace = namespace;
struct_->cname = "gpointer";
namespace->structs = g_list_append (namespace->structs, struct_);
vala_context_add_symbols_from_namespace (context, namespace);
/* namespace GLib */
......@@ -375,7 +347,7 @@ vala_context_resolve_type_reference (ValaContext *context, ValaNamespace *namesp
if (type_reference->type_name == NULL) {
/* var type, resolve on initialization */
return;
} else if (type_reference->namespace_name == NULL) {
} else if (type_reference->namespace_name == NULL || strlen (type_reference->namespace_name) == 0) {
/* no namespace specified */
/* check for generic type parameter */
......@@ -429,7 +401,7 @@ vala_context_resolve_type_reference (ValaContext *context, ValaNamespace *namesp
ns_symbol = g_hash_table_lookup (context->root->symbol_table, type_reference->namespace_name);
if (ns_symbol == NULL) {
/* specified namespace not found */
err (type_reference->location, "error: specified namespace '%s' not found", type_reference->namespace_name);