Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DataLiteralBase.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Globalization;
6 
7 namespace Irony.Parsing {
8 
9  //DataLiteralBase is a base class for a set of specialized terminals with a primary purpose of building data readers
10  // DsvLiteral is used for reading delimiter-separated values (DSV), comma-separated format is a specific case of DSV
11  // FixedLengthLiteral may be used to read values of fixed length
12  public class DataLiteralBase : Terminal {
13  public TypeCode DataType;
14  //For date format strings see MSDN help for "Custom format strings", available through help for DateTime.ParseExact(...) method
15  public string DateTimeFormat = "d"; //standard format, identifies MM/dd/yyyy for invariant culture.
16  public int IntRadix = 10; //Radix (base) for numeric numbers
17 
18  public DataLiteralBase(string name, TypeCode dataType) : base(name) {
19  DataType = dataType;
20  }
21 
22  public override Token TryMatch(ParsingContext context, ISourceStream source) {
23  try {
24  var textValue = ReadBody(context, source);
25  if (textValue == null) return null;
26  var value = ConvertValue(context, textValue);
27  return source.CreateToken(this.OutputTerminal, value);
28  } catch(Exception ex) {
29  //we throw exception in DsvLiteral when we cannot find a closing quote for quoted value
30  return source.CreateErrorToken(ex.Message);
31  }
32  }//method
33 
34 
35  protected virtual string ReadBody(ParsingContext context, ISourceStream source) {
36  return null;
37  }
38 
39  protected virtual object ConvertValue(ParsingContext context, string textValue) {
40  switch(DataType) {
41  case TypeCode.String: return textValue;
42  case TypeCode.DateTime: return DateTime.ParseExact(textValue, DateTimeFormat, context.Culture);
43  case TypeCode.Single:
44  case TypeCode.Double:
45  var dValue = Convert.ToDouble(textValue, context.Culture);
46  if (DataType == TypeCode.Double) return dValue;
47  return Convert.ChangeType(dValue, DataType, context.Culture);
48 
49  default: //integer types
50  var iValue = (IntRadix == 10)? Convert.ToInt64(textValue, context.Culture) : Convert.ToInt64(textValue, IntRadix);
51  if (DataType == TypeCode.Int64) return iValue;
52  return Convert.ChangeType(iValue, DataType, context.Culture);
53  }
54  }//method
55 
56  }//class
57 
58 }//namespace
Interface for Terminals to access the source stream and produce tokens.
DataLiteralBase(string name, TypeCode dataType)
override Token TryMatch(ParsingContext context, ISourceStream source)
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
virtual string ReadBody(ParsingContext context, ISourceStream source)
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
virtual object ConvertValue(ParsingContext context, string textValue)