edbee - Qt Editor Library
textgrammar.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <QHash>
9 #include <QList>
10 #include <QMap>
11 #include <QString>
12 #include <QStringList>
13 
14 class QFile;
15 
16 namespace edbee {
17 
18 class RegExp;
19 class TextGrammar;
20 class Edbee;
21 
22 
25 {
26 public:
27 
29  enum Instruction {
36  };
37 
38 
41 
42  static TextGrammarRule* createMainRule( TextGrammar* grammar, const QString& scopeName );
44  static TextGrammarRule* createSingleLineRegExp( TextGrammar* grammar, const QString& scopeName, const QString& regExp );
45  static TextGrammarRule* createMultiLineRegExp( TextGrammar* grammar, const QString& scopeName, const QString& contentScopeName, const QString& beginRegExp, const QString& endRegExp );
47 
48  inline bool isMainRule() { return instruction_ == MainRule; }
49  inline bool isRuleList() { return instruction_ == RuleList; }
50  inline bool isMultiLineRegExp() { return instruction_ == MultiLineRegExp; }
51  inline bool isSingleLineRegExp() { return instruction_ == SingleLineRegExp; }
52  inline bool isIncludeCall() { return instruction_ == IncludeCall; }
53 
54  int ruleCount() const { return ruleList_.size(); }
55  TextGrammarRule* rule( int idx ) const;
56  void giveRule( TextGrammarRule* rule );
57 
58  void giveMatchRegExp( RegExp* regExp );
59  void setEndRegExpString( const QString& str );
60 
61  Instruction instruction() const { return instruction_; }
62  void setInstruction( Instruction ins ) { instruction_ = ins; }
63  QString scopeName() const { return scopeName_; }
64  void setScopeName( const QString& scopeName ) { scopeName_ = scopeName; }
65  RegExp* matchRegExp() const { return matchRegExp_; }
66  QString endRegExpString() const { return endRegExpString_; }
67  const QMap<int,QString>& matchCaptures() { return matchCaptures_; }
68  const QMap<int,QString>& endCaptures() { return endCaptures_; }
69  QString contentScopeName() const { return contentScopeName_; }
70  void setContentScopeName( const QString& name) { contentScopeName_ = name; }
71 
73  QString includeName() { return contentScopeName_; }
74  void setIncludeName( const QString& name ) { contentScopeName_ = name; }
75 
76  void setCapture( int idx, const QString& name ) { matchCaptures_.insert(idx,name); }
77  void setEndCapture( int idx, const QString& name ) { endCaptures_.insert(idx,name); }
78 
79  QString toString(bool includePatterns=true);
80 
81 
82  // An itetor class for iterating over the ruleset (todo template this)
83  class Iterator
84  {
85  public:
86  Iterator( const TextGrammarRule* rule ) : index_(0), ruleRef_(rule){}
87  bool hasNext() { return index_ < ruleRef_->ruleCount(); }
88  TextGrammarRule* next() { return ruleRef_->rule(index_++); }
89  private:
90  int index_;
91  const TextGrammarRule* ruleRef_;
92  };
93 
94  Iterator* createIterator() { return new Iterator(this); }
95 
96  TextGrammar* grammar() { return grammarRef_; }
97 
98 private:
99 
100  static RegExp* createRegExp( const QString& regexp );
101 
102 
103 private:
104  TextGrammar* grammarRef_;
105  Instruction instruction_;
106  QString scopeName_;
107 
108  RegExp* matchRegExp_;
109  //RegExp* endRegExp_; ///< The end regular expression matcher
110  QString endRegExpString_;
111 
112  QMap<int,QString> matchCaptures_;
113  QMap<int,QString> endCaptures_;
114 
115  QString contentScopeName_;
116 
117  QList<TextGrammarRule*> ruleList_;
118 };
119 
120 
121 //================================
122 
123 
126 {
127 public:
128 
129  TextGrammar( const QString& name, const QString& displayName );
130  ~TextGrammar();
131 
132  void giveMainRule( TextGrammarRule* mainRule );
133 
134  QString name() const;
135  QString displayName() const;
136  TextGrammarRule* mainRule() const;
137  QStringList fileExtensions() const;
138 
139  void giveToRepos( const QString& name, TextGrammarRule* rule);
140  TextGrammarRule* findFromRepos( const QString& name, TextGrammarRule* defValue = 0 );
141  void addFileExtension( const QString& ext );
142 
143 private:
144  QString name_;
145  QString displayName_;
146  TextGrammarRule *mainRule_;
147  QMap<QString, TextGrammarRule*> repository_;
148  QStringList fileExtensions_;
149 };
150 
151 
152 
153 //================================
154 
155 
158 {
159 protected:
161  virtual ~TextGrammarManager();
162 
163 public:
164  TextGrammar* readGrammarFile(const QString& file );
165  void readAllGrammarFilesInPath(const QString& path );
166 
167  TextGrammar* get( const QString& name );
168  void giveGrammar( TextGrammar* grammar );
169 
170  QList<QString> grammarNames();
171  QList<TextGrammar*> grammars();
172  QList<TextGrammar*> grammarsSortedByDisplayName();
173 
174  TextGrammar* defaultGrammar() { return defaultGrammarRef_; }
175  TextGrammar* detectGrammarWithFilename( const QString& fileName );
176 
177  QString lastErrorMessage() const;
178 
179 private:
180 
181  TextGrammar* defaultGrammarRef_;
182  QMap<QString,TextGrammar*> grammarMap_;
183  QString lastErrorMessage_;
184 
185  friend class Edbee;
186 };
187 
188 } // edbee
bool hasNext()
Definition: textgrammar.h:87
bool isMultiLineRegExp()
Definition: textgrammar.h:50
static TextGrammarRule * createMainRule(TextGrammar *grammar, const QString &scopeName)
Creates a main grammar rule.
Definition: textgrammar.cpp:43
void setEndRegExpString(const QString &str)
Sets the ends regular expression(only for multi-line regexp rules.
Definition: textgrammar.cpp:131
QString contentScopeName() const
Definition: textgrammar.h:69
Includes another scope.
Definition: textgrammar.h:34
void setInstruction(Instruction ins)
Definition: textgrammar.h:62
static TextGrammarRule * createRuleList(TextGrammar *grammar)
Creates a grammar rule list.
Definition: textgrammar.cpp:54
The texteditor manager, It manages all singleton objects for the editor It performs the initializatio...
Definition: edbee.h:24
const QMap< int, QString > & matchCaptures()
Definition: textgrammar.h:67
int ruleCount() const
Definition: textgrammar.h:54
Iterator(const TextGrammarRule *rule)
Definition: textgrammar.h:86
Definition: textgrammar.h:83
~TextGrammarRule()
The tex text grammar rule.
Definition: textgrammar.cpp:31
defines a single grammar rule
Definition: textgrammar.h:24
TextGrammarRule * rule(int idx) const
returns the child rule at the given index
Definition: textgrammar.cpp:106
bool isSingleLineRegExp()
Definition: textgrammar.h:51
TextGrammar * grammar()
Definition: textgrammar.h:96
void setEndCapture(int idx, const QString &name)
Definition: textgrammar.h:77
Copyright 2011-2013 - Reliable Bits Software by Blommers IT.
Definition: commentcommand.cpp:22
A class for matching QStrings with the Oniguruma API We need this Regular Expression library to be ab...
Definition: regexp.h:36
static TextGrammarRule * createIncludeRule(TextGrammar *grammar, const QString &includeName)
Creates an include rule.
Definition: textgrammar.cpp:65
A list of rules (no regexp)
Definition: textgrammar.h:31
This class defines a single language grammar.
Definition: textgrammar.h:125
QString endRegExpString() const
Definition: textgrammar.h:66
TextGrammarRule * next()
Definition: textgrammar.h:88
void setContentScopeName(const QString &name)
Definition: textgrammar.h:70
The main rule has no regexp matches.
Definition: textgrammar.h:30
const QMap< int, QString > & endCaptures()
Definition: textgrammar.h:68
Instruction
the instructions
Definition: textgrammar.h:29
void giveMatchRegExp(RegExp *regExp)
Gives the main regular expression.
Definition: textgrammar.cpp:123
bool isIncludeCall()
Definition: textgrammar.h:52
void setCapture(int idx, const QString &name)
Definition: textgrammar.h:76
QString scopeName() const
Definition: textgrammar.h:63
RegExp * matchRegExp() const
Definition: textgrammar.h:65
bool isRuleList()
Definition: textgrammar.h:49
Instruction instruction() const
Definition: textgrammar.h:61
void setIncludeName(const QString &name)
Definition: textgrammar.h:74
static TextGrammarRule * createSingleLineRegExp(TextGrammar *grammar, const QString &scopeName, const QString &regExp)
Creates an single line regexp.
Definition: textgrammar.cpp:78
static TextGrammarRule * createMultiLineRegExp(TextGrammar *grammar, const QString &scopeName, const QString &contentScopeName, const QString &beginRegExp, const QString &endRegExp)
Creates a multiline regexp.
Definition: textgrammar.cpp:93
QString toString(bool includePatterns=true)
convers the curent object toString (for debugging purposes)
Definition: textgrammar.cpp:139
This class is used to manage all &#39;grammers&#39; used by the lexers.
Definition: textgrammar.h:157
QString includeName()
the include name is stored in the content-scopename
Definition: textgrammar.h:73
void setScopeName(const QString &scopeName)
Definition: textgrammar.h:64
void giveRule(TextGrammarRule *rule)
Gives the rulle to the rule-list of the grammar rule.
Definition: textgrammar.cpp:115
A full parser (not yet supported, by added as idea for the future). But could be marked by multiple r...
Definition: textgrammar.h:35
A single line regexp.
Definition: textgrammar.h:32
TextGrammarRule(TextGrammar *grammar, Instruction instruction)
The text grammar rule constructor.
Definition: textgrammar.cpp:21
bool isMainRule()
Definition: textgrammar.h:48
A multi-line regexp (begin end)
Definition: textgrammar.h:33
TextGrammar * defaultGrammar()
Definition: textgrammar.h:174
Iterator * createIterator()
Definition: textgrammar.h:94