X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FParse.cs;h=ca98353067cf4f55e0ad499aa235082d1e4e32a7;hp=e1a482eb6a08c55398650186bdefd3f45d9fd527;hb=236bc590ff21370c1139a8c01ff35f7b30af743d;hpb=c5daf1d9adca0c3a826dfa2ac7b6d4f8a64c84a3
diff --git a/CarFire/CarFire/CarFire/Parse.cs b/CarFire/CarFire/CarFire/Parse.cs
index e1a482e..ca98353 100644
--- a/CarFire/CarFire/CarFire/Parse.cs
+++ b/CarFire/CarFire/CarFire/Parse.cs
@@ -9,7 +9,8 @@ namespace CarFire
{
///
/// Class with handy static methods taking strings and returning objects
- /// parsed from those strings.
+ /// parsed from those strings. For all of these functions, white space is
+ /// generally ignored, but superfluous characters are not allowed.
///
public class Parse
{
@@ -25,6 +26,18 @@ namespace CarFire
return null;
}
+ ///
+ /// Parses a comment of an INI file.
+ ///
+ /// Text.
+ /// The comment, or null if parsing failed.
+ public static string IniComment(string line)
+ {
+ Match match = Regex.Match(line, @"^;\s*(.*)\s*$");
+ if (match.Success) return match.Groups[1].Value;
+ return null;
+ }
+
///
/// Parses a key-value pair.
///
@@ -96,26 +109,36 @@ namespace CarFire
return null;
}
+ ///
+ /// Parses a single character.
+ ///
+ /// Text.
+ /// The character, or null if parsing failed.
+ public static char? Char(string atom)
+ {
+ string str = String(atom);
+ if (str != null && str.Length == 1) return str[0];
+ return null;
+ }
+
///
/// Parses a constant from an enum.
///
/// An enumeration.
/// Text.
/// The constant, or null if parsing failed.
- public static T Constant(string atom)
+ public static T? Constant(string atom) where T : struct
{
- string constant = String(atom);
- if (constant != null)
+ try
{
- foreach (string enumConstant in System.Enum.GetNames(typeof(T)))
- {
- if (constant == enumConstant)
- {
- return (T)System.Enum.Parse(typeof(T), constant);
- }
- }
+ return (T)System.Enum.Parse(typeof(T), String(atom));
+ }
+#pragma warning disable 0168
+ catch (System.Exception ex)
+#pragma warning restore 0168
+ {
+ return null;
}
- return default(T);
}
///
@@ -158,7 +181,7 @@ namespace CarFire
/// Parses a function.
///
/// Text.
- /// An array two strings containing the function name and
+ /// An array with two strings containing the function name and
/// parameter-list, in that order, or null if parsing failed.
public static string[] Function(string atom)
{
@@ -171,17 +194,6 @@ namespace CarFire
return null;
}
- ///
- /// Parses a comma-separated list of atoms.
- ///
- /// Text.
- /// An array of strings containing the atoms, or null
- /// if parsing failed.
- public static string[] ParameterList(string text)
- {
- return null;
- }
-
///
/// Parses a whitespace-separated list of atoms.
///
@@ -195,7 +207,6 @@ namespace CarFire
// FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.
foreach (Match match in matches)
{
- Console.WriteLine("matched: " + match.Value);
list.Add(match.Value);
}