|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace BoneSoft.CSS {
public class CSSParser {
private List<string> errors = new List<string>();
private CSS doc;
public CSS ParseFile(string file) {
StringBuilder sb = new StringBuilder();
TextWriter errorCatch = new StringWriter(sb);
Scanner scanner = new Scanner(file);
Parser parser = new Parser(scanner);
parser.errors.errorStream = errorCatch;
parser.Parse();
doc = parser.CSSDocument;
SpitErrors(sb);
return doc;
}
public List<Token> GetTokens(string file) {
StringBuilder sb = new StringBuilder();
TextWriter errorCatch = new StringWriter(sb);
Scanner scanner = new Scanner(file);
List<Token> ts = new List<Token>();
Token t = scanner.Scan();
if (t.val != "\0") { ts.Add(t); }
while (t.val != "\0") {
t = scanner.Scan();
ts.Add(t);
}
return ts;
}
public CSS ParseText(string content) {
MemoryStream mem = new MemoryStream();
byte[] bytes = ASCIIEncoding.ASCII.GetBytes(content);
mem.Write(bytes, 0, bytes.Length);
return ParseStream(mem);
}
public CSS ParseStream(Stream stream) {
StringBuilder sb = new StringBuilder();
TextWriter errorCatch = new StringWriter(sb);
Scanner scanner = new Scanner(stream);
Parser parser = new Parser(scanner);
parser.errors.errorStream = errorCatch;
parser.Parse();
doc = parser.CSSDocument;
SpitErrors(sb);
return doc;
}
public CSS CSSDocument {
get { return doc; }
}
public List<string> Errors {
get { return errors; }
}
private void SpitErrors(StringBuilder sb) {
errors = new List<string>();
string text = sb.ToString().Replace("\r", "");
if (text.Length == 0) { return; }
string[] lines = text.Split('\n');
foreach (string line in lines) {
errors.Add(line);
}
}
/*
System.Text.StringBuilder sb = new StringBuilder();
System.IO.TextWriter errorCatch = new System.IO.StringWriter(sb);
Scanner scanner = new Scanner(ofd.FileName);
Parser parser = new Parser(scanner);
parser.errors.errorStream = errorCatch;
parser.Parse();
doc = parser.CSSDocument;
*/
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been in software development for more than a decade now. Originally with ASP 2.0 and VB6. I worked in Japan for a year doing Java. And have been with C# ever since.
In 2005 I founded
BoneSoft Software where I sell a small number of developer tools.