medit/moo/mooedit/moolang.h

299 lines
9.7 KiB
C
Raw Normal View History

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4; coding: utf-8 -*-
*
* moolang.h
*
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* 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 2 of the License, or
* (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#ifndef __MOO_LANG_H__
#define __MOO_LANG_H__
#include <gtk/gtktexttag.h>
#include <mooedit/mootextstyle.h>
G_BEGIN_DECLS
#define MOO_LANG_DIR_BASENAME "syntax"
#define MOO_STYLES_PREFS_PREFIX MOO_EDIT_PREFS_PREFIX "/styles"
#define MOO_TYPE_LANG (moo_lang_get_type ())
#define MOO_TYPE_LANG_TABLE (moo_lang_table_get_type ())
#define MOO_LANG_TABLE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MOO_TYPE_LANG_TABLE, MooLangTable))
#define MOO_LANG_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOO_TYPE_LANG_TABLE, MooLangTableClass))
#define MOO_IS_LANG_TABLE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MOO_TYPE_LANG_TABLE))
#define MOO_IS_LANG_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_LANG_TABLE))
#define MOO_LANG_TABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_LANG_TABLE, MooLangTableClass))
typedef struct _MooLang MooLang;
typedef struct _MooLangTable MooLangTable;
typedef struct _MooLangTableClass MooLangTableClass;
typedef struct _MooContext MooContext;
typedef struct _MooContextArray MooContextArray;
typedef struct _MooCtxSwitch MooCtxSwitch;
typedef struct _MooRule MooRule;
typedef struct _MooRuleArray MooRuleArray;
struct _MooLang {
MooLangTable *table;
GHashTable *context_names; /* char* -> MooContext* */
MooContextArray *contexts;
GHashTable *style_names; /* char* -> MooStyle* */
MooTextStyleArray *styles;
GHashTable *style_cache; /* char* -> MooStyle* */
char *name; /* not NULL */
char *section; /* not NULL; "Others" by default */
char *version; /* not NULL; "" by default */
char *author; /* not NULL; "" by default */
GSList *mime_types; /* list of mime types */
GSList *extensions; /* list of globs */
char *brackets;
char *single_line_comment;
char *multi_line_comment_start;
char *multi_line_comment_end;
guint hidden : 1;
};
struct _MooLangTable {
GObject base;
GSList *lang_dirs;
GSList *langs;
GHashTable *lang_names;
GHashTable *schemes;
MooTextStyleScheme *active_scheme;
guint dirs_read : 1;
};
struct _MooLangTableClass
{
GObjectClass base_class;
void (*lang_added) (MooLangTable *table,
const char *lang_name);
void (*lang_removed) (MooLangTable *table,
const char *lang_name);
};
typedef enum {
MOO_CONTEXT_STAY = 0,
MOO_CONTEXT_POP,
MOO_CONTEXT_SWITCH
} MooCtxSwitchType;
struct _MooCtxSwitch {
MooCtxSwitchType type;
union {
guint num;
MooContext *ctx;
};
};
struct _MooRuleArray {
MooRule **data;
guint len;
};
struct _MooContextArray {
MooContext **data;
guint len;
};
struct _MooContext {
MooLang *lang;
char *name;
char *style;
MooRuleArray *rules;
MooCtxSwitch line_end;
};
typedef enum {
MOO_RULE_ASCII_STRING,
MOO_RULE_REGEX,
MOO_RULE_ASCII_CHAR,
MOO_RULE_ASCII_2CHAR,
MOO_RULE_ASCII_RANGE,
MOO_RULE_KEYWORDS,
MOO_RULE_INCLUDE
} MooRuleType;
typedef enum {
MOO_RULE_MATCH_FIRST_CHAR = 1 << 0,
MOO_RULE_MATCH_FIRST_NON_EMPTY_CHAR = 1 << 1,
MOO_RULE_MATCH_FIRST_LINE = 1 << 2,
MOO_RULE_MATCH_CASELESS = 1 << 3,
MOO_RULE_INCLUDE_INTO_NEXT = 1 << 4
} MooRuleFlags;
typedef struct {
char *string;
guint length;
guint caseless : 1;
} MooRuleAsciiString;
typedef struct {
gpointer regex; /* EggRegex* */
guint non_empty : 1;
} MooRuleRegex;
typedef struct {
char ch;
guint caseless : 1;
} MooRuleAsciiChar;
typedef struct {
char str[3];
} MooRuleAscii2Char;
typedef struct {
char *chars;
guint n_chars;
} MooRuleAsciiRange;
typedef struct {
MooContext *ctx;
} MooRuleInclude;
struct _MooRule
{
char *style;
MooContext *context;
MooCtxSwitch exit;
MooRuleType type;
MooRuleFlags flags;
MooRuleArray *child_rules;
union {
MooRuleAsciiString str;
MooRuleRegex regex;
MooRuleAsciiChar _char;
MooRuleAscii2Char _2char;
MooRuleAsciiRange range;
MooRuleInclude incl;
};
};
GType moo_lang_get_type (void) G_GNUC_CONST;
GType moo_lang_table_get_type (void) G_GNUC_CONST;
/*****************************************************************************/
/* MooContext
*/
void moo_context_add_rule (MooContext *ctx,
MooRule *rule);
void moo_context_set_line_end_stay (MooContext *ctx);
void moo_context_set_line_end_pop (MooContext *ctx,
guint num);
void moo_context_set_line_end_switch (MooContext *ctx,
MooContext *target);
/*****************************************************************************/
/* MooLang
*/
MooLang *moo_lang_new (MooLangTable *table,
const char *name,
const char *section,
const char *version,
const char *author);
char *moo_lang_get_name (MooLang *lang);
char *moo_lang_get_display_name (MooLang *lang);
MooLang *moo_lang_ref (MooLang *lang);
void moo_lang_unref (MooLang *lang);
MooContext *moo_lang_add_context (MooLang *lang,
const char *name,
const char *style);
MooContext *moo_lang_get_context (MooLang *lang,
const char *ctx_name);
MooContext *moo_lang_get_default_context (MooLang *lang);
void moo_lang_add_style (MooLang *lang,
const char *name,
const MooTextStyle *style);
/*****************************************************************************/
/* MooLangTable
*/
MooLangTable *moo_lang_table_new (void);
GSList *moo_lang_table_get_available_langs (MooLangTable *table);
GSList *moo_lang_table_get_sections (MooLangTable *table);
MooLang *moo_lang_table_get_lang_for_file (MooLangTable *table,
const char *filename);
MooLang *moo_lang_table_get_lang_for_filename (MooLangTable *table,
const char *filename);
MooLang *moo_lang_table_get_lang_for_mime_type (MooLangTable *table,
const char *mime_type);
MooLang *moo_lang_table_get_lang (MooLangTable *table,
const char *name);
MooContext *moo_lang_table_get_context (MooLangTable *table,
const char *lang_name,
const char *ctx_name);
void moo_lang_table_add_dir (MooLangTable *table,
const char *dir);
void moo_lang_table_read_dirs (MooLangTable *table);
MooTextStyle *moo_lang_table_get_style (MooLangTable *table,
const char *lang_name, /* default style if NULL */
const char *style_name);
void moo_lang_table_set_style (MooLangTable *table,
const char *lang_name, /* default style if NULL */
const char *style_name,
const MooTextStyle *style);
MooTextStyleScheme *moo_lang_table_get_active_scheme (MooLangTable *table);
/*****************************************************************************/
/* Auxiliary private methods
*/
void _moo_lang_set_tag_style (MooLang *lang,
GtkTextTag *tag,
MooContext *ctx,
MooRule *rule);
void _moo_style_set_tag_style (const MooTextStyle *style,
GtkTextTag *tag);
/* implemented in moohighlighter.c */
MooContext *_moo_text_iter_get_context (const GtkTextIter *iter);
G_END_DECLS
#endif /* __MOO_LANG_H__ */