Last active
December 21, 2017 22:36
-
-
Save mbdavid/9bf14848e0ea01dbf607347cd9221090 to your computer and use it in GitHub Desktop.
LiteDB Unity changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REPLACE "Reflection.Expression.cs" | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace LiteDB | |
{ | |
/// <summary> | |
/// Unity3D version without any Emit | |
/// </summary> | |
internal partial class Reflection | |
{ | |
public static CreateObject CreateClass(Type type) | |
{ | |
return () => Activator.CreateInstance(type); | |
} | |
public static CreateObject CreateStruct(Type type) | |
{ | |
return () => Activator.CreateInstance(type); | |
} | |
public static GenericGetter CreateGenericGetter(Type type, MemberInfo memberInfo) | |
{ | |
// when member is a field, use simple Reflection | |
if (memberInfo is FieldInfo) | |
{ | |
var fieldInfo = memberInfo as FieldInfo; | |
return fieldInfo.GetValue; | |
} | |
// if is property, use Emit IL code | |
var propertyInfo = memberInfo as PropertyInfo; | |
var getMethod = propertyInfo.GetGetMethod(true); | |
if (getMethod == null) return null; | |
return target => getMethod.Invoke(target, null); | |
} | |
public static GenericSetter CreateGenericSetter(Type type, MemberInfo memberInfo) | |
{ | |
// when member is a field, use simple Reflection | |
if (memberInfo is FieldInfo) | |
{ | |
var fieldInfo = memberInfo as FieldInfo; | |
return fieldInfo.SetValue; | |
} | |
// if is property, use Emit IL code | |
var propertyInfo = memberInfo as PropertyInfo; | |
var setMethod = propertyInfo.GetSetMethod(true); | |
if (setMethod == null) return null; | |
return (target, value) => setMethod.Invoke(target, new[] { value }); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REPLACE "StringScanner.cs" | |
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace LiteDB | |
{ | |
/// <summary> | |
/// A StringScanner is state machine used in text parsers based on regular expressions | |
/// </summary> | |
public class StringScanner | |
{ | |
public string Source { get; private set; } | |
public int Index { get; set; } | |
/// <summary> | |
/// Initialize scanner with a string to be parsed | |
/// </summary> | |
public StringScanner(string source) | |
{ | |
this.Source = source; | |
this.Index = 0; | |
} | |
public override string ToString() | |
{ | |
return this.HasTerminated ? "" : this.Source.Substring(this.Index); | |
} | |
/// <summary> | |
/// Reset cursor position | |
/// </summary> | |
public void Reset() | |
{ | |
this.Index = 0; | |
} | |
/// <summary> | |
/// Skip cursor position in string source | |
/// </summary> | |
public void Seek(int length) | |
{ | |
this.Index += length; | |
} | |
/// <summary> | |
/// Indicate that cursor is EOF | |
/// </summary> | |
public bool HasTerminated | |
{ | |
get { return this.Index >= this.Source.Length; } | |
} | |
/// <summary> | |
/// Scan in current cursor position for this patterns. If found, returns string and run with cursor | |
/// </summary> | |
public string Scan(string pattern) | |
{ | |
return this.Scan(new Regex((pattern.StartsWith("^") ? "" : "^") + pattern, RegexOptions.IgnorePatternWhitespace)); | |
} | |
/// <summary> | |
/// Scan in current cursor position for this patterns. If found, returns string and run with cursor | |
/// </summary> | |
public string Scan(Regex regex) | |
{ | |
var match = regex.Match(this.ToString()); | |
if (match.Success) | |
{ | |
this.Index += match.Length; | |
return match.Value; | |
} | |
else | |
{ | |
return string.Empty; | |
} | |
} | |
/// <summary> | |
/// Scan pattern and returns group string index 1 based | |
/// </summary> | |
public string Scan(string pattern, int group) | |
{ | |
return this.Scan(new Regex((pattern.StartsWith("^") ? "" : "^") + pattern, RegexOptions.IgnorePatternWhitespace), group); | |
} | |
public string Scan(Regex regex, int group) | |
{ | |
var match = regex.Match(this.ToString()); | |
if (match.Success) | |
{ | |
this.Index += match.Length; | |
return group >= match.Groups.Count ? "" : match.Groups[group].Value; | |
} | |
else | |
{ | |
return string.Empty; | |
} | |
} | |
/// <summary> | |
/// Match if pattern is true in current cursor position. Do not change cursor position | |
/// </summary> | |
public bool Match(string pattern) | |
{ | |
return this.Match(new Regex((pattern.StartsWith("^") ? "" : "^") + pattern, RegexOptions.IgnorePatternWhitespace)); | |
} | |
/// <summary> | |
/// Match if pattern is true in current cursor position. Do not change cursor position | |
/// </summary> | |
public bool Match(Regex regex) | |
{ | |
var match = regex.Match(this.ToString()); | |
return match.Success; | |
} | |
/// <summary> | |
/// Throw syntax exception if not terminate string | |
/// </summary> | |
public void ThrowIfNotFinish() | |
{ | |
this.Scan(@"\s*"); | |
if (!this.HasTerminated) throw LiteException.SyntaxError(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment